0

I'm able to import an RNA sequence (eg. AUGCCGACCCGCAGUCCCAGCG) and define a dictionary for translating it into a protein sequence (eg. AUG = M, CCG = Q, ACC = T). Every 3 letters of RNA sequence codes for one letter of protein sequence, and this translation is defined in the dictionary. How do I get Python to read the RNA sequence and output the protein sequence?

I've tried this code:

for i in range(0, len(rna_sequence), 3): 
    codon_dictionary = codon_dictionary + rna_sequence[i:i+3]

When I run it, nothing happens. No error message.

Edit: I forgot to add that I want the program to print the resulting protein sequence. I think RNA_sequence is defined as a string.

Moo
  • 11
  • 1
  • 2
    When I copy/paste this code and run it, I get an error because `codon_dictionary` is undefined. Please provide a [mcve] for us to help you. – Code-Apprentice Jun 20 '23 at 04:26
  • Also, what do you expect to happen when you run your code? Do you include any `print()` statements? If not, you won't have any output to see the result. – Code-Apprentice Jun 20 '23 at 04:26
  • `protein = ''.join([codon_dictionary[rna_sequence[i:i+3]] for i in range(0, len(rna_sequence), 3)])` – mozway Jun 20 '23 at 04:38
  • Before this part of the code runs, what is `rna_sequence`? What is `codon_dictionary`? Most importantly, **exactly how** do you want the program to communicate a result? Should `codon_dictionary` change when the code runs? Should a new value be created? If there should be a new value, what name should it have? Then, think carefully about the logic, step by step. What should the result be if there is nothing in the `rna_sequence`? When a value from the `rna_sequence` is "translated", what should happen to the result value? – Karl Knechtel Jun 20 '23 at 04:39

1 Answers1

-1

Pass RNA codons as keys to the dictionary to access needed amino acids, save all amino acids iteratively in a list and then you can join the list into a string to get a string sequence.

protein_sequence = []
for i in range(0, len(rna_sequence), 3):     
    protein_sequence.append(codon_dictionary[rna_sequence[i:i+3]])
print(''.join(protein_sequence))