为什么我的DNA模式Python代码无法提供正确的输出?

时间:2018-11-03 11:16:57

标签: python python-3.x

如果我输入“ ATACTCGTCGTCGATCGATACTCGTCTGTCGTCGAGTCGTTCGTCTCGTC”作为dna,并输入“ TCGTC”作为模式,则应输出

____ * __ * _____________ * ______ * ___________ * ____ * ____在重叠模式的开始位置标记一个星星。

enter image description here

但是我的代码没有给我正确的输出,为什么?

这是我的代码:

def printMatch(dna,pattern):
    for i in range(0,len(dna)):
      if dna[i:len(pattern)]!=pattern:
        dna+="_"
      else:
        dna+="*"
    print(dna)

def main():
    dna=input()
    pattern=input()
    printMatch(dna,pattern)

main()

1 个答案:

答案 0 :(得分:3)

问题在这里:dna[i:len(pattern)]:之后的值是结束索引,而不是子字符串的长度。而是这样做:dna[i:i+len(pattern)]