String not recognized in if statement

Screenshot 2024-09-28 133118

Hi !

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 !

Please provide the code as pre-formatted text, use the </> button, not as a screen shot.

I cannot copy and paste from a screen shot to try your code.

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.

1 Like

As an aside, you can just use the find method, which returns -1 when the given string isn’t found.

>>> "xxAUGyz".find("AUG")
2
>>> "xxCCCzy".find("AUG")
-1

As such, you can define a similar function using operator.methodcaller.

start_codon = methodcaller('find', "AUG")

Thank you so much ! It works perfectly now !