How do I check for multiple exceptions in Python?
DNASequence = "laksjfklsajdfklsajfklasjfklsad"
while True:
lMerLength = input("Please enter the length of the l-mers of the universal array :")
try:
if len(DNASequence) >= lMerLength > 0:
break
except SyntaxError:
pas开发者_如何学Cs
#This is not working. How do I check for multiple exceptions in Python?
except NameError:
pass
print "ERROR: Please check your input. You entered an invalid input."
Here is how you check for multiple exceptions.
try:
..............
except (SyntaxError, NameError, ...):
..............
finally:
.............
The problem is, that input
returns a string and you compare that string in your if to an int. And in python 2.x you should use raw_input
instead of input
:
DNASequence = "laksjfklsajdfklsajfklasjfklsad"
while True:
try:
lMerLength = int(raw_input("Please enter the length of the l-mers of the universal array :"))
except ValueError:
print "ERROR: Please check your input. You entered an invalid input."
continue
if len(DNASequence) >= lMerLength > 0:
break
print "ERROR: Please check your input. You entered an invalid input."
精彩评论