[英]Identify stop codons
I was trying to write some code that identifies stop codons in my sequence. 我试图编写一些代码来识别序列中的终止密码子。 I wrote the following code, but when I try it out, it stops after I type in my sequence when prompted. 我写了以下代码,但是当我尝试一下时,在提示时键入序列后它停止了。 I am not sure why it doesn't go through. 我不确定为什么它没有通过。 Thanks a lot! 非常感谢!
dna=input("Please type your DNA sequence: ")
stopcodons=("TAG","TGA","TAA")
pos=0
while (pos+2)<(len(dna)):
if dna[pos:pos+3] in stopcodons:
type("Your sequence has a stop codon at %s position." %pos)
else:
pos=pos+3
your loop is never going to end if you find a position because you do not increment pos. 如果找不到位置,则循环永远不会结束,因为您不会增加pos。 Also, I'm not sure why you increment by 3 pos. 另外,我不确定您为什么要增加3 pos。
while (pos+2)<(len(dna)):
if dna[pos:pos+3] in stopcodons:
type("Your sequence has a stop codon at %s position." %pos)
pos=pos+1
should work better 应该更好地工作
The reason why your program goes into an infinite loop once it has found a stop codon, is because you instruct your program to only increment pos
(and thus move the cursor ) in the else
case : when you did not found a sequence. 一旦找到终止密码子,程序便进入无限循环的原因是,在else
情况下,当您未找到序列时, 您指示程序仅递增pos
(并因此移动光标 ) 。
You thus have to update the cursor every time: 因此,您每次必须更新游标:
while (pos+2)<(len(dna)):
if dna[pos:pos+3] in stopcodons:
print("Your sequence has a stop codon at %s position." %pos)
pos=pos+3
(or even better: use pos += 3
instead of pos = pos + 3
). (甚至更好:使用pos += 3
代替pos = pos + 3
)。
You also have to use print
instead of type
since type
simply obtains the type of an object. 您还必须使用print
而不是type
因为type
只是获取对象的类型。
Note that it is usually safer to use a range(..)
in a for
loop, like: 请注意,在for
循环中使用range(..)
通常较为安全,例如:
for pos in range(0,len(dna),3):
if dna[pos:pos+3] in stopcodons:
print("Your sequence has a stop codon at %s position." %pos)
You almost NEVER want to use a counter, or index into arrays/strings directly in Python. 您几乎永远都不想使用计数器,或者直接在Python中索引到数组/字符串。
dna=input("Please type your DNA sequence: ")
stopcodons=("TAG","TGA","TAA")
for stopcodon in stopcodons:
if stopcodon in dna:
print("Your sequence has a stop codon at %s position" % dna.find(stopcodon))
The above is more "Pythonic" and helps to prevent numerous mistakes that are all too common in less advanced languages. 上面的内容更像是“ Pythonic”,有助于防止在较不高级的语言中太常见的众多错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.