How can't the codes below work, in order to get the complement of the character entered? It seems like the loop never end, but let say, if I enter 'Z' as dna, why wouldn't it break and quit? Did I use the break or if wrongly? How about elif?
def get_complement(dna):
''' (ch) -> ch
Reverse the 'A' to 'T' or vice versa and 'C' to 'G' and vice versa too.
>>> get_complement('A')
'C'
>>> get_complement('G')
'T'
'''
if dna == 'A':
print ('C')
if dna == 'C':
print ('A')
if dna == 'T':
print ('G')
if dna == 'G' :
print ('T')
while {'A', 'C', 'G', 'T'}.isnotsubset(set(dna)) :
break
return ('')
You should set up a map, using a dictionary
complement = {'A': 'C', 'C': 'A', 'T': 'G', 'G': 'T'}
Then for some string you can do
original = "ATCGTCA"
"".join(complement[letter] for letter in original)
Output
'CGATGAC'
For just a single character:
complement['A']
Output
'C'