[Tutor] GC content: Help with if/else statements:
Mihir Kharate
kharatemihir at gmail.com
Sun Oct 13 01:17:31 EDT 2019
Hi,
Following is part of the code I am working on in Python3. I am trying to
calculate the GC content of the input DNA sequence. I want the function to
return "Invalid base-pair in your sequence" if the input sequence has
letters other than A, T, G and C.
######################################################
input_sequence = input("Input your sequence here: ")
input_sequence = input_sequence.upper()
print("\nYour sequence is: " + input_sequence)
print("\n")
a=t=g=c=0
def gc_content():
global a,g,t,c
for character in input_sequence:
if character == 'G':
g+=1
if character == 'C':
c+=1
if character == 'T':
t+=1
if character == 'A':
a+=1
gc_cont = (g+c)/(a+g+t+c)*100
if character == 'A' or 'T' or 'G' or 'C':
print ("The GC content is: " + str(gc_cont) + " %")
else :
print("Invalid base-pair in your Sequence")
gc_content()
############################################
*Question 1*: When I use the else statement as I have in the code above, it
is ineffective. Even if I input other letters in my input sequence, it
still returns the GC content.
*Question 2*: When I use another if statement like below, it returns the GC
content and "Invalid base-pair in your Sequence" both. No matter whether
the input has any other characters than A,T,G,C or not.
if character == 'A' or 'T' or 'G' or 'C':
print ("The GC content is: " + str(gc_cont) + " %")
if character is not 'A' or 'T' or 'G' or 'C':
print("Invalid base-pair in your Sequence")
*Question 3*: How do I fix this?
Thank you for any suggestions and help!
~Mihir
More information about the Tutor
mailing list