How to check user input in python

I am new to python and I am trying to figure out how to check user input. I want to ask the user to submit the DNA sequence and confirm that it is a DNA sequence. Acceptable inputs can have upper or lower case ATCG and spaces, and I don’t know exactly how to do this.

So far I can request input, but not test it.

import sys
Var1 = raw_input("Enter Sequence 1:")

Then I want to do something like:

if Var1 != ATCG (somehow put 'characters that match ATCG or space)
    print "Please input valid DNA sequence"
    sys.exit() (to have it close the program)

Any help? I feel this should be pretty simple, but I don't know how to indicate that it can be any ATCG, atcg or space.

+3
source share
2 answers

You can use all, str.lowerand the expression:

if not all(x in "agct " for x in Var1.lower()):
    print "Please input valid DNA sequence"
    sys.exit(1) 

, Var1 :

"A", "T", "C", "G", " ", "a", "t", "c", "g"
+4

( Python, , ?):

>>> import re
>>> def special_match(strg, search=re.compile(r'[^atcgATCG\s]').search):
...   return bool(search(strg))
>>> if (special_match("atcF")):
...   print "Invalid input"
...
>>> Invalid input
+1

All Articles