This is my first every question here on this website. For a bit of background I am very interested in applying machine learning in preventative medicine as I believe this is what the future of medicine has in store. For this reason I have been teaching myself python via Rosalind.info. One question had us creating a function that can calculate the GC content of a sequence. One issue that I had was that when I used the first code, it evaluated to one. However, when I used the second code, it evaluated to the correct answer. If anyone can explain why this is the case that'd be much appreciated!
First try:
n = input("Paste in sequence here!").upper()
def cg_content(sequence):
gc_count = 0
total = len(sequence)
for base in sequence:
if base =='C' or 'G':
gc_count += 1
else:
gc_count = gc_count
percentage = float(gc_count)/float(total)
print(percentage)
cg_content(n)
Second Try:
n = input("Paste in sequence here!").upper()
def cg_content(sequence):
gc_count = 0
total = len(sequence)
for base in sequence:
if base =='C' or base == 'G':
gc_count += 1
else:
gc_count = gc_count
percentage = float(gc_count)/float(total)
print(percentage)
cg_content(n)
I know it has something to do with the 'or' statement but I thought that both statements are essentially equivalent regardless of whether the '==' was there once or twice.