I just started learning Python, and I'm stuck in this problem: I have a DNA sequence, and I need to return its complementary sequence. For example, if I have ATTGCA, it should return TAACGT. This is, replace A by T, T by A, C by G and G by C. It's an exercise, and I'm not supposed to use string methods. Everything I tried until now, return me 'T' as answer. Seems it only recognize the first letter, then stops. How can I do it?
I tried:
>>> def get_complementary_sequence(dna):
for char in dna:
if char == 'A':
return 'T'
elif char == 'T':
return 'A'
elif char == 'C':
return 'G'
elif char == 'G':
return 'C'
>>> get_complementary_sequence('ATTGCA')
'T'
And also tried:
def get_complementary_sequence(dna):
sequence = ""
for nucleotide in dna:
if nucleotide == 'A':
return sequence + 'T'
elif nucleotide == 'T':
return sequence + 'A'
elif nucleotide == 'C':
return sequence + 'G'
elif nucleotide == 'G':
return sequence + 'C'
return sequence
>>> get_complementary_sequence('ATTGCA')
'T'
Everything I tried until now-> then show us what you tried?