python - 在 Python 2.7 中,如何将用户输入限制为特定整数并跟踪异常?

标签 python python-2.7 validation

编辑:建议的副本对于基本输入验证非常有帮助。虽然它确实涵盖了很多内容,但我的具体问题(未能将 int(evaluation) 分配给变量)仅在此处明确解决。我单独标记这一点,以防其他人犯了类似的愚蠢错误:)

过去几周我一直在玩 Python 2.7,并且玩得很开心。为了了解有关 while 循环的更多信息,我创建了一个小脚本,要求用户输入 1 到 10 之间的整数。

我的目标是能够响应用户使用意外输入(例如非整数或指定范围之外的整数)进行响应的情况。在其他 StackOverflow 线程的帮助下,我已经解决了很多问题,但现在我陷入了困境。

首先,我创建了一个变量idiocy来跟踪异常。 (这个脚本应该很时髦,但在我让它发挥作用之前,我就是它取笑的那个人。)

idiocy = 0

while 1:
    evaluation = raw_input("> ")
    try:
        int(evaluation)
        if evaluation < 1 or evaluation > 10:
            raise AssertionError
    except ValueError:
        idiocy += 1
        print "\nEnter an INTEGER, dirtbag.\n"
    except AssertionError:
        idiocy += 1
        print "\nI said between 1 and 10, moron.\n"
    else:
        if idiocy == 0:
            print "\nOkay, processing..."
        else:
            print "\nDid we finally figure out how to follow instructions?"
            print "Okay, processing..."
        break

如您所见,我正在尝试处理两个不同的错误 - 输入类型的 ValueError 和整数范围的 AssertionError - 以及记录他们被提出的次数。 (实际上,我只关心知道它们是否至少被提出过一次;这就是我侮辱用户所需要的。)

无论如何,当我以当前形式运行脚本时,错误响应工作得很好(“dirtbag”表示非整数,“moron”表示超出范围)。问题是,即使我输入一个有效的整数,我仍然得到一个超出范围的 AssertionError

我怀疑我的问题与我的 while 逻辑有关,但我不确定该怎么做。我在这里或那里添加了一个break,但这似乎没有帮助。有什么建议或明显的错误吗?再说一遍,这里完全是 Python 初学者,所以我只是半即兴发挥。

//如果有人有更简单、更干净或更漂亮的方法来做到这一点,请随时告诉我。我是来学习的!

最佳答案

您的问题是您没有将 evaluationint 版本保存到 evaluation,如下所示:

idiocy = 0

while 1:
    evaluation = raw_input("> ")
    try:
        evaluation = int(evaluation) <--- here
        if evaluation < 1 or evaluation > 10:
            raise AssertionError
    except ValueError:
        idiocy += 1
        print "\nEnter an INTEGER, dirtbag.\n"
    except AssertionError:
        idiocy += 1
        print "\nI said between 1 and 10, moron.\n"
    else:
        if idiocy == 0:
            print "\nDid we finally figure out how to follow instructions?"
            print "Okay, processing..."
        else:
            print "\nOkay, processing..."

如果您想跟踪引发的异常类型,您可以使用 collections.Counter 来表示 idiocy 并更改代码,如下所示:

from collections import Counter

idiocy = Counter()

while 1:
    evaluation = raw_input("> ")
    try:
        evaluation = int(evaluation)
        if evaluation < 1 or evaluation > 10:
            raise AssertionError
    except ValueError as e:
        idiocy[e.__class__] += 1 
        print "\nEnter an INTEGER, dirtbag.\n"
    except AssertionError as e:
        idiocy[e.__class__] += 1
        print "\nI said between 1 and 10, moron.\n"
    else:
        if idiocy == 0:
            print "\nDid we finally figure out how to follow instructions?"
            print "Okay, processing..."
        else:
            print "\nOkay, processing..."

>>> idiocy
Counter({AssertionError: 2, ValueError: 3})

您可以通过键访问错误计数,例如idiocy[AssertionError]

关于python - 在 Python 2.7 中,如何将用户输入限制为特定整数并跟踪异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45725552/

相关文章:

python - Openshift 是否支持 Selenium ?

ruby-on-rails - 创建 mongoid Rails 后跳过验证

python - Azure Functions Python 应用程序 - 启用 IdentityModelEventSource.ShowPII 属性

python - Python 中的三元运算符中的多个变量赋值可能吗?

list - 将列表中的多个元素添加到 list.count (Python)

python - selenium.common.exceptions.WebDriverException : Message: 'firefox' executable needs to be in PATH with GeckoDriver Firefox Selenium and Python

Python OpenCV 如何在转换后保存图像

unit-testing - 如何测试来自Playground/Validator的验证错误?

html - 验证 JSP 和 HTML 表单,服务器端或客户端,或两者?

python 多个QInputDialog