This is the code that I have been trying to use but it does not work properly:
mrna = input("Please enter mRNA sequence: ")
start = mrna.find('AUG')
if start != -1:
while start + 2 < len(mrna):
codon = mrna[start:start + 3]
if codon == "UAA":
break
print(codon)
start += 3
Expected start codon: AUG
Expected stop codon: UAA or UAG or UGA If for example:
input = "AUGUGA"
output = 1
input = "GAGAUGUUGGUUUAA"
output = 3
I don't really know what's wrong.
You aren't displaying numbers anywhere in your code. The number of codons is the difference between the end and the start index, floor divided by 3.
You can use a generator to help you check the codons.
mrna = input("Please enter mRNA sequence: ")
start = mrna.find('AUG')
if start != -1:
end, last = next((x, mrna[x:x + 3] for x in range(start + 3, len(mrna) - 3, 3) if mrna[x:x + 3] in ('UAA', 'UAG', 'UGA')), (len(mrna), 'end'))
print(f'{(end - start) // 3} codons between AUG and {last}')
else:
print('AUG not found')
The generator (x, mrna[x:x + 3] for x in range(start + 3, len(mrna) - 3, 3) if mrna[x:x + 3] in ('UAA', 'UAG', 'UGA'))
steps over all codons from index start + 3
to the end and yields codons that match anything in ('UAA', 'UAG', 'UGA')
, along with the index. next
normally returns the next (first) element of an iterator. With the second argument, it returns the extra argument as a sentinel instead of raising a StopIteration
when the iterator runs out. //
is the truncation division operator, so it will work correctly even if len(mrna)
is not a multiple of 3 away from start
.