编辑:建议的副本在基本输入验证方面非常有用。虽然它确实涵盖了很多,但我的具体问题(未能将while
分配给变量)仅在此处明确说明。如果其他人犯了同样愚蠢的错误,我会单独标记这个:)
我在过去的几周里一直在玩Python 2.7并且玩得很开心。要了解有关idiocy
循环的更多信息,我创建了一个小脚本,要求用户提供1到10之间的整数。
我的目标是能够响应用户以意外输入响应的情况,例如非整数或超出指定范围的整数。我已经能够在其他StackOverflow线程的帮助下修复我的很多问题,但现在我已经难倒了。
首先,我创建了一个变量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
,整数范围为AssertionError
- 并跟踪他们很多次被提出来。 (真的,我只关心他们是否至少被提过一次;这就是我需要侮辱用户的一切。)
无论如何,当我以当前形式运行脚本时,错误响应工作正常(&#39; dirtbag&#39;对于非整数,&#39; moron&#39;对于超出范围) 。问题是即使我输入一个有效的整数,我仍然会得到一个超出范围的while
。
我怀疑我的问题与我的break
逻辑有关,但我不知道该怎么做。我在这里或那里添加了{{1}},但这似乎没有帮助。有任何建议或明显错误吗?再一次,这里是Python的初学者,所以我对它进行了一半的抨击。
//如果有人有更简单,更清洁或更漂亮的方法,请随时告诉我。我在这里学习!
答案 0 :(得分:2)
您的问题是,您没有将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..."
,如下所示:
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]
您可以按{{1}}
等键访问错误计数答案 1 :(得分:1)
您有int(evalutation)
,但您没有将其分配给任何内容。
尝试
try:
evaluation = int(evaluation)
assert 0 < evaluation < 10
except ValueError:
...
except AssertionError:
答案 2 :(得分:0)
您的范围测试可以重构为
assert 1 <= evaluation <= 10
你可以将你的侮辱保存在字典中
insults = {AssertionError : "\nI said between 1 and 10, moron.\n",
ValueError : "\nEnter an INTEGER, dirtbag.\n"
}
然后写下try / except
try:
...
except (AssertionError, ValueError) as e:
print(insults[type(e)])
将用户输入更改为int时,需要将其分配给
evaluation = int(evaluation)
assert用于调试 - 您使用不正确。
答案 3 :(得分:0)
在你的代码中:
int(evaluation)
不是将评估变量类型转换为int类型。输出是:
> 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
希望它有所帮助。