2

Can someone tell me why when I try out this function with this particular example: is_valid_sequence('ABCDEFG'), it comes up "True" instead of "False"? I am completely new to programming in general. This is for an online course I'm taking. Thanks.

def is_valid_sequence(dna_sequence):
 ''' (str) -> bool
 Return True if and only if DNA sequence is made up of 'A', 'T', 'C', and 'G' nucleotides.
 >>> is_valid_sequence('ATCGGC')
 True
 >>> is_valid_sequence('aTcGGc')
 False
 >>> is_valid_sequence('ABCDEFG')
 False
 ''' 
 for char in dna_sequence:
 if char not in 'ACTG':
 return False
 else:
 return True
Gabe
87.2k13 gold badges144 silver badges239 bronze badges
asked Oct 22, 2012 at 5:00

6 Answers 6

11

You are returning too early. Try this

for char in dna_sequence:
 if char not in 'ACTG':
 return False
return True

or more simply

return all(char in 'ACTG' for char in dna_sequence)
answered Oct 22, 2012 at 5:03
Sign up to request clarification or add additional context in comments.

2 Comments

all is perhaps more pythonic.
Upvoted for 'all', and all I can say is wow! Sunday midnight, and 6 responses before my answer is submitted -- all within 8 minutes. I'm gonna just sit back and wait henceforth ...
3

Your method will exit at the first character that matches. Since the first character of ABCDEFG is a valid character, your method return True.

You need to go through the entire string and see if all characters match.

answered Oct 22, 2012 at 5:04

Comments

2

Because you use return only the first char gets tested.

Simple correction:

res = True
for ch in dna_sequence:
 if ch not in 'ACTG':
 res &= False # or return as you know it's false
 else:
 res &= True
return res

But there are more "pythonic" way to do this, take a look at the all() function for example

answered Oct 22, 2012 at 5:03

3 Comments

Yes, I imagined this was the problem. But how do I fix it?
Would you really use &= like that? BTW you forgot to return res
No, that was for the explanation, I would go with all()
1

What happens is that as soon as it hits the A, it returns True. Your logic needs to not return until it hits either an invalid character or the end of the string:

 for char in dna_sequence:
 if char not in 'ACTG':
 return False
 # we hit the end of the string, so it must be valid
 return True
answered Oct 22, 2012 at 5:04

Comments

0

In your loop, you're returning True if the first value matches, rather than examining all the values. Corrected code:

for char in dna_sequence:
 if char not in 'ACTG':
 return False
return True
answered Oct 22, 2012 at 5:08

Comments

0

return causes an exit of the function regardless of anything this will only check one character

for char in dna_sequence:
 if char not in 'ACTG':
 return False
return true # after whole string checked 
answered Oct 22, 2012 at 5:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.