For example, I want to exclude inputs other than "G,T,A and C" from an input. How do I do this?
So, this is what I'm working on right now.:
a=input('What is your DNA sequence?: ')
I need to exclude inputs other than "G,T,A and C" from this.
You could use regular expressions for filtering. The most simple example would be:
import re
x = input("What is your DNA sequence?: ")
r = re.compile('^[CAGT]+$')
while not r.match(x):
print ("DNA sequence '" + x + "' invalid")
x = input("What is your DNA sequence?:")
Here regexp ^[CAGT]+$ means that input should match characters C, A, G, or T, which are repeated (+) from the beginning of the string (^) to its end ($).
You can try:
tokens = {'G', 'T', 'A', 'C'}
while True:
inp = input('>>')
if inp == 'quit':
break
if not set(inp).issubset(tokens):
print('invalid tokens')
else:
print('valid')
Sample test cases:
>>ATGCCTGGGTACTAA
valid
>>AGTweddc
invalid tokens
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With