Python 这个函数我做错了什么?

Python 这个函数我做错了什么?,python,Python,我不知道我做了什么-这是错误的。 有人能帮我吗 def insert_sequence(dna1, dna2, number): '''(str, str, int) -> str Return the DNA sequence obtained by inserting the second DNA sequence at the given index. (You can assume that the index is valid.) >

我不知道我做了什么-这是错误的。 有人能帮我吗

def insert_sequence(dna1, dna2, number):

    '''(str, str, int) -> str
    Return the DNA sequence obtained by inserting the second DNA sequence
    at the given index. (You can assume that the index is valid.)  

    >>> insert_sequence('CCGG', 'AT', 2)
    'CCATGG'
    >>> insert_sequence('TTGC', 'GG', 2)
    'TTGGGC'
    '''
    index = 0
    result = '';
    for string in dna1:
        if index == number:
            result = result + dna2

            result = result + string
            index += 1

            print(result)

您永远不会将字符串分开,因此始终将dna2前置到dna1

您可能希望
返回dna1[:number]+dna2+dna1[number:][/code>

以下是一个解决方案:

def insert_sequence(dna1, dna2, number):

    '''(str, str, int) -> str
    Return the DNA sequence obtained by inserting the second DNA sequence
    at the given index. (You can assume that the index is valid.)  

    >>> insert_sequence('CCGG', 'AT', 2)
    'CCATGG'
    >>> insert_sequence('TTGC', 'GG', 2)
    'TTGGGC'
    '''

    return dna1[:number] + dna2 + dna1[number:]

这里需要一个
if-else
循环:

def insert_sequence(dna1, dna2, number):


    result = '';

    #you can use enumerate() to keep track of index you're on

    for ind,x in enumerate(dna1): 
        if ind == number:            #if index is equal to number then do this
            result = result + dna2 +x
        else:                        #otherwise do this   
            result = result + x 


    print(result)

insert_sequence('CCGG', 'AT', 2)
insert_sequence('TTGC', 'GG', 2)
输出:

CCATGG
TTGGGC

如果索引不在插入点,则不执行任何操作,包括增加索引。您的代码需要一个else,而且您还提前打印:

def insert_sequence(dna1, dna2, number):
    index = 0
    result = '';
    for char in dna1:
        if index == number:
            result = result + dna2
            result = result + char
            index += len(dna2) + 1
        else:
            result = result + char
            index += 1
    print(result)

其他答案中已经有正确的工作函数(特别是Rakesh Pandit的评论和JeffS的答案),但您实际的问题是“为什么我的原始函数不工作”

我复制了您的函数的工作版本,注释如下:

def insert_sequence(dna1, dna2, number):

    index = 0
    result = ''

    for character in dna1:
        if index == number:
            result = result + dna2
        result = result + character
        index += 1
    print(result)
Python考虑缩进,因此您应该只在事物的末尾、循环和ifs之外打印。 当您“增加”您的结果时,您只在函数的“if”内这样做,而实际上您应该增加“对于dna1中的每个字符”,并且只有当/“if index==number”时,您才应该将中间的字符串放入

我相信您对Python或一般编程非常陌生,可能来自生物学背景,但您确实不应该像其他人所展示的那样通过迭代来完成这种类型的字符串操作

希望这有帮助

出错:a)参数索引初始化为0。b) 对于dia1中的sting:“应该是”对于范围内的dia1_位置(len(dia1)):“c)打印结果缩进是错误的,函数不应该只是打印。它应该返回结果。d) 现在不需要增加索引


答案已经存在。上面简要列出了所犯的错误。我猜您没有看到任何错误,因为您从未调用该函数。第一个错误应该是“数字”未定义(不再是,因为问题已更新,参数已定义数字)。

是否失败?怎么用?您是否收到错误消息(是什么消息)?您可以返回dna1[:index]+dna2+dna1[index:]当我尝试示例函数时,什么都没有发生,我不知道我做了什么欢迎使用堆栈溢出。很高兴您发布了代码,但我们还需要知道您看到了哪些错误。此外,请在信用到期时给予信用。(参见)尽管我是第一个给你一个线性答案的人,但每个人都复制了,但忘记给你错误的信息:a)参数索引初始化为0。b) 对于dia1中的sting:“应该是”对于范围内的dia1_位置(len(dia1)):“c)打印结果缩进是错误的,函数不应该只是打印。它应该返回结果。d) 现在不需要增加索引。来吧,我先给你这个答案;)@写下答案,这样他就可以接受了!(也许下次吧)!值得一提的是,有更高效、干净、类似python的方法来完成这一点……但我想OP对python来说是新的,我们最好尝试指出他的代码中的错误,而不是给他一行。我的意思是应该指出错误,应该给出固定的函数,以及更好的整体解决方案的一些方向,例如所说的一行。是的,现在是这样。早些时候,我读到问题索引代替了数字。这个问题似乎已经更新了。我将更新该部分。非常感谢。