Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python2.7中,如何将用户输入限制为特定整数,并跟踪异常?_Python_Python 2.7_Validation - Fatal编程技术网

在Python2.7中,如何将用户输入限制为特定整数,并跟踪异常?

在Python2.7中,如何将用户输入限制为特定整数,并跟踪异常?,python,python-2.7,validation,Python,Python 2.7,Validation,编辑:建议的副本在基本输入验证方面非常有用。虽然它涵盖了很多内容,但我的具体问题(未能将int(evaluation)赋值给变量)仅在这里明确说明。我将分别对此进行标记,以防其他人犯下类似的愚蠢错误:) 过去几周我一直在玩Python2.7,玩得很开心。为了进一步了解while循环,我创建了一个小脚本,要求用户输入1到10之间的整数 我的目标是能够响应用户使用意外输入(如非整数或指定范围外的整数)进行响应的情况。在其他StackOverflow线程的帮助下,我已经能够修复我的许多问题,但现在我被

编辑:建议的副本在基本输入验证方面非常有用。虽然它涵盖了很多内容,但我的具体问题(未能将
int(evaluation)
赋值给变量)仅在这里明确说明。我将分别对此进行标记,以防其他人犯下类似的愚蠢错误:)

过去几周我一直在玩Python2.7,玩得很开心。为了进一步了解
while
循环,我创建了一个小脚本,要求用户输入1到10之间的整数

我的目标是能够响应用户使用意外输入(如非整数或指定范围外的整数)进行响应的情况。在其他StackOverflow线程的帮助下,我已经能够修复我的许多问题,但现在我被难住了

首先,我创建了一个变量,
idiocy
,以跟踪异常。(剧本本来应该很时髦,但在我开始工作之前,我就是它取笑的那个人。)

白痴=0
而1:
评估=原始输入(“>”)
尝试:
内部培训(评估)
如果评估<1或评估>10:
提出断言错误
除值错误外:
白痴+=1
打印“\n输入一个整数,dirtbag。\n”
除断言错误外:
白痴+=1
打印“\n我说的是1到10之间,笨蛋。\n”
其他:
如果白痴==0:
打印“\nOkay,正在处理…”
其他:
打印“\n我们是否最终确定了如何遵循说明?”
打印“好的,正在处理…”
打破
如您所见,我试图处理两个不同的错误——输入类型为
ValueError
,整数范围为
AssertionError
——并跟踪它们被引发的次数。(真的,我只关心他们是否至少被抚养过一次;这就是我要侮辱用户的全部。)

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

我怀疑我的问题与我的
while
逻辑有关,但我不确定该怎么办。我在这里或那里添加了一个
break
,但这似乎没有帮助。有什么建议或明显的错误吗?再说一次,这里是Python的初学者,所以我有点勉强


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

您的问题是您没有将
int
版本的
evaluation
保存到
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..."
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]

您有
int(evalutation)
,但您没有将其分配给任何对象

试一试

试试看:
评估=int(评估)
断言0<评估<10
除值错误外:
...
除断言错误外:

您的范围测试可以重构为

assert 1 <= evaluation <= 10
然后像这样写try/except

try:
    ...
except (AssertionError, ValueError) as e:
    print(insults[type(e)])
当您将用户输入更改为int时,需要将其分配给某个对象

evaluation = int(evaluation)

是用于调试的-您使用不正确

  • 您应该使用非整数重复-类型错误
  • 用于范围之外的响应-值错误
在您的代码中:
int(求值)
不是将求值变量类型转换为int类型。输出为:

> 2
<type 'str'>
I said between 1 and 10, moron.
>2
我说1到10之间,白痴。
试试这个:

idiocy = 0

while 1:
    try:
        evaluation = int(raw_input("> "))
        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
白痴=0
而1:
尝试:
evaluation=int(原始输入(“>”)
如果评估<1或评估>10:
提出断言错误
除值错误外:
白痴+=1
打印“\n输入一个整数,dirtbag。\n”
除断言错误外:
白痴+=1
打印“\n我说的是1到10之间,笨蛋。\n”
其他:
如果白痴==0:
打印“\nOkay,正在处理…”
其他:
打印“\n我们是否最终确定了如何遵循说明?”
打印“好的,正在处理…”
打破
顺便说一下,您可以使用tuple存储所有异常。 例如:

idiocy = 0

all_exceptions = (ValueError, AssertionError)
while 1:
    try:
        evaluation = int(raw_input("> "))
        if evaluation < 1 or evaluation > 10:
            raise AssertionError("\nI said between 1 and 10, moron.\n")
    except all_exceptions as e:
        idiocy += 1
        print str(e)
    else:
        if idiocy == 0:
            print "\nOkay, processing..."
        else:
            print "\nDid we finally figure out how to follow instructions?"
            print "Okay, processing..."
        break
白痴=0
所有_异常=(ValueError,AssertionError)
而1:
尝试:
evaluation=int(原始输入(“>”)
如果评估<1或评估>10:
提出断言错误(“\n我说的是1到10之间,笨蛋。\n”)
除所有例外情况外,例如:
白痴+=1
打印str(e)
其他:
如果白痴==0:
打印“\nOkay,正在处理…”
其他:
打印“\n我们是否最终确定了如何遵循说明?”
打印“好的,正在处理…”
打破

希望能有所帮助。

我会停止使用python 2.7,而专注于python 3,如果可以的话,最好是3.6.2版。离2.7的生命终结只有几年的时间了,如果你能从正确的方向开始,而不必重新学习其中的一些细节,那将是一件好事。我在Zed Shaw臭名昭著的分裂性书籍的帮助下学习了@MatthewPurdon的可能翻版,这本书只涉及2.7,因为我喜欢它的格式。话虽如此,我以前也考虑过迁移到3.x。我会四处看看,但是你有没有什么[更新的]电子书或网站可以推荐我入门?你的最后一点特别有用,谢谢。更改错误类型使逻辑更加清晰——我现在就这么做。这就是问题所在。回想起来似乎很明显!谢谢。还有一件事——将
int(evaluation)
赋值给
evaluation
,而不是使用唯一的变量名,这是一种糟糕的形式吗?看起来可能会让人困惑,除非我遗漏了什么。虽然有点冗长,但实际上并没有什么问题。如果你想剪的话
> 2
<type 'str'>
I said between 1 and 10, moron.
idiocy = 0

while 1:
    try:
        evaluation = int(raw_input("> "))
        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
idiocy = 0

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