【发布时间】:2015-09-30 08:34:03
【问题描述】:
参加入门课程并需要创建一个过度/不足猜谜游戏。如果有人输入负数或非整数,我想通过创建错误来微调我的用户输入。我正确报告了非整数错误,并且负数正确循环返回,但负数不会打印我的错误消息。
#Number of plays
def get_plays(msg):
while True:
try:
x = (int(input(msg)))
except ValueError:
print ("Integer numbers only please.")
except:
if x <=0:
print ("Positive numbers only please.")
i = get_plays("\nHow many times would you like to play?")
print ("The game will play " +str(i)+" times.")
另外,如果我想使用类似的设置来为 1 到 20 之间的任何非整数负数产生错误,这会是什么样子?
【问题讨论】:
-
您认为
except:块将执行什么输入? -
将
if语句移动到try部分可能。
标签: python python-3.x error-handling