【发布时间】:2014-12-07 04:41:30
【问题描述】:
我从 python 开始。当我将新的 raw_input 放入 if 语句中时,我不断收到错误消息。如果他们回答的输入不正确,我想再给他们一次输入更多输入的机会,还有其他方法吗?
代码如下
attraction = {
'approach' : "words",
'introduction' : "words",
'qualification' : "words",
'rapport' : "words"
}
answer = raw_input("Pick from these approaches: introduction, qualification, or rapport:")
if answer != "approach" and answer != "introduction" and answer != "qualification" and answer != "rapport":
new_answer = raw_input("You didn't choose one of the choices, type in the choice you want again:")
if new_answer != "approach" and answer != "introduction" and answer != "qualification" and answer != "rapport":
print "That was your last chance buddy"
else:
print attraction[answer]
【问题讨论】:
-
将原始输入放在一个while循环中。有答案时使用 break
-
我知道这不是一个相关的问题,但是,请问您为什么开始学习 Python 2.7?为什么不是 Python 3?
-
与您的问题无关:写起来更容易:
if answer not in ("approach", "introduction", "qualification", "rapport"): -
我认为你应该使用
or表达式而不是 `and' 因为单个输入不能是 3 种不同的类型。 -
你也可以
if answer not in attraction.keys():
标签: python if-statement input