I am trying to create a definition allowing the user to find the position of the start codon in a sequence.
My variable s_codon is supposed to include three letters at position (0,3), then move on to the following at position (1,4), then (2,5), etc, and check if s_codon equals to “AUG” (the start codon).
It seems that the if statement can never recognize “AUG” : the start position is always returned as the length of the sequence. Any ideas as to what I could add to fix this ?
Thank you !
Oh your loop always goes to the end and stops with start is len(mRNA).
What is the condition for returning? Is it the if?
in that case instead put a break or a return in the if.
Barry identified the issue: your for loop doesn’t stop, it just keeps adding 1 to the start position for every non-start-codon. To stop once you’ve found AUG, use a break:
for start in range(len(mRNA)): # don't need to add 1, the last positions can't match anyway
if mRNA[start: start + 3] == "AUG":
break
But I’d suggest a much simpler solution to this problem anyway: use str.find, which will return the index of the first start codon, or -1 if it does not exist in the string.