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

发布于 2024-11-06 12:19:21 字数 521 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

注定孤独终老 2024-11-13 12:19:21

以下是检查多个异常的方法。

尝试:

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

除了(语法错误,名称错误,...):

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

最后:

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

Here is how you check for multiple exceptions.

try:

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

except (SyntaxError, NameError, ...):

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

finally:

    .............
む无字情书 2024-11-13 12:19:21

问题是,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."

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."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文