如何在Python中检查多个异常?

时间:2011-05-11 05:15:21

标签: exception-handling python-2.6

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."

2 个答案:

答案 0 :(得分:2)

以下是检查多个例外情况的方法。

尝试:

    ..............

除了(SyntaxError,NameError,...):

    ..............

最后:

    .............

答案 1 :(得分:0)

问题是,input返回一个字符串,并将if中的字符串与int进行比较。在python 2.x中,您应该使用raw_input而不是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."