如何在 Python 中检查多个异常?
DNA 序列 = "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:
pass
#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."
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:
pass
#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."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是检查多个异常的方法。
尝试:
除了(语法错误,名称错误,...):
最后:
Here is how you check for multiple exceptions.
try:
except (SyntaxError, NameError, ...):
finally:
问题是,
input
返回一个字符串,并且您将 if 中的该字符串与 int 进行比较。在 python 2.x 中,您应该使用raw_input
而不是input
: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 useraw_input
instead ofinput
: