Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 代码循环不正确_Python_Loops - Fatal编程技术网

Python 代码循环不正确

Python 代码循环不正确,python,loops,Python,Loops,这段代码应该以更大的顺序(称为sequentie)打印所有编码序列。 然而,它正在无限地打印第一个找到的序列 def getCodons(sequentie): DNA = sequentie while 'atg' in DNA: start = DNA.find('atg') codon = getSequentie(DNA, start) DNA = DNA[DNA.index(codon):] def getSequen

这段代码应该以更大的顺序(称为sequentie)打印所有编码序列。 然而,它正在无限地打印第一个找到的序列

def getCodons(sequentie):
    DNA = sequentie
    while 'atg' in DNA:
        start = DNA.find('atg')
        codon = getSequentie(DNA, start)
        DNA = DNA[DNA.index(codon):]


def getSequentie(DNA, start):
    print ('')
    print ("Sequentie:")
    while start+2 < len(DNA):
            codon = DNA[start:start+3]
            if codon == "tag" or codon == "taa" or codon == "tga":
                print (codon, end=' ')
                return codon
                break
            print (codon, end=' ')
            start+=3    
def getcodon(顺序):
DNA=序列
而DNA中的“atg”:
start=DNA.find('atg'))
密码子=getSequentie(DNA,起始)
DNA=DNA[DNA.索引(密码子):]
def getSequentie(DNA,开始):
打印(“”)
打印(“序列:”)
启动时+2
因此,输出:

顺序: 自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱

顺序: 自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱

顺序: 自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱

顺序: 自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱 ...

接下来 CCAGATGTTACTACACTCGCCACACACACACACACATACTACTACTTATCTATCTATCTATCTATCTATCTATCTATCTATCCAATCCAGTATCCAATTCCAATTCTATCTATCTATCTATCGATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCCATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCTATCGATCTATCTATCGATCTATCTATCTATCTATCGATCTATCTATCTATCTATCTATCTATCTATCTATCTATTGTTCTAAGAAGAAGAGATCTTTTTTTGTGGATTTCTCTTTCAGGAGAGCGGTTTAGGGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGATCTAGATCTAGATCGATTCATTCATTCTATTTTTTTTTTTTGTGCAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGATCGATCGATCGAAAGAGAGAGAGAGAGAG

我想去 顺序: 自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱自动变速箱


然后找到下一个序列。主要的问题是你一直在外循环中找到相同的“atg”实例。这是因为getSequentie返回一个总是以“atg”开头的字符串。因此在循环的下一次迭代中,start将为0(DNA字符串以“atg”开头)。从那一刻起,
DNA.index(codon)
将始终产生零,因此在下一次迭代中不会发生任何变化,从而导致无限循环

您可以通过在此处添加1来解决此问题:

DNA = DNA[DNA.index(codon)+1:]
这样,DNA总是会变短,因此循环是有限的

您还应该定义在找不到结束密码子时希望发生的事情。在这种情况下,第二个函数将返回
None
,第一个函数应该处理这个问题(应该退出)。在这种情况下,可能不应该打印序列

最好推迟打印,直到得到最终结果。第二个函数可以返回序列结束的索引(如果没有找到结束密码子,则返回
None
)。然后,您可以用序列填充结果变量,并将打印留给调用方

下面是应用这些更改和其他一些更改后的代码:

def getCodons(sequentie):
    DNA = sequentie
    start = 0
    codons = []
    while 'atg' in DNA:
        start = DNA.find('atg', start)
        end = getSequentie(DNA, start)
        if not end:
            break
        codons.append(' '.join([DNA[i:i+3] for i in range(start, end, 3)]))
        start = end
    return codons


def getSequentie(DNA, start):
    for start in range(start, len(DNA)-2, 3):
        if DNA[start:start+3] in ("tag", "taa", "tga"):
            return start+3

sequences = getCodons('ccagaatggttactatggacatccgccaaccatacaagctatggtgaaatgctttatctatctcatttttagtttcaaagcttttgttataacacatgcaaatccatatccgtaaccaatatccaatcgcttgacatagtctgatgaagtttttggtagttaagataaagctcgagactgatatttcatatactggatgatttagggaaacttgcattctattcatgaacgaatgagtcaatacgagacacaaccaagcatgcaaggagctgtgagttgatgttctatgctatttaagtatttttcgggagatatatatatcttattgttctcctcctcccgagtcaagttgttctaagaaagaaggatctatttcattttgtggattgtctagtttcagggacagacggggtttaggggaagcgctatccgtggctgctatgacatcgaagaaactctgcacgacatggtatgtaatct')

print('\n'.join(sequences))

查看它的运行情况。

您是否可以添加一个示例输入和预期输出
getCodons
您可能需要
DNA=DNA[DNA.index(codon)+1://code>您是否可以提供一个具体的示例输入和预期输出?否则代码的目的很难实现。顺便说一句,我认为您必须编写
start+=4
而不是
start+=3
此代码(第二个函数:getSequentie)在另一个脚本中运行良好,该脚本只需要第一个sequencein第一个函数中的第一个sequencein,您没有在循环中设置atg,是吗?