2017-04-14 55 views
-1

我正在使用Python 2.7.13和Windows Powershell。当我运行程序时,无论是否输入“是”,“是”或“是”,都不会识别第一个raw_input()Python While Loop不注册来自以前变量的raw_input

理想情况下,我希望程序注册是否有任何这些选项是最初使用的。如果其中一个选项未被使用,while循环将继续在准备好开始时显示“Type'yes'消息,直到从用户接收到适当的输入为止。任何帮助将不胜感激!

# This is a questionnaire form for a python programming exercise. 

print "Hello, Sir or Madame.", 
print "My name is Oswald, and I will be asking you a few questions, today." 
print "Are you ready to begin?" 
answer_1 = raw_input() 

while answer_1 != "yes" or answer_1 != "y" or answer_1 != "Yes": 
    print "Type 'yes' when you are ready to begin." 
    answer_1 = raw_input() 

    if (answer_1 == "yes") or (answer_1 == "y") or (answer_1 == "Yes"): 
     print "What is your name" 
     name = raw_input() 
     print "What is your favorite color?" 
     color = raw_input() 
     print "Where do you live?" 
     home = raw_input() 
     print "So, you're %r. You're favorite color is %r, and you live in %r" % (name, color, home) 
+1

的可能的复制[为什么一个变量的非平等对证许多值总是返回true?(http://stackoverflow.com/questions/26337003 /为什么 - 非平等 - 检查 - 的 - 一个变量,对一对多值,总是返回真) – Barmar

+0

您可以用'answer.lower()',以确保输入的是小写不管用户输入什么内容。 – Astrom

回答

-1

实际上,你可以简化你想要做什么:

answer_1 = '' 
while answer_1.lower() not in ('y', 'yes', 'si', 'da', 'yes, please'): 
    answer_1 = raw_input('Are you ready to begin? (type "yes" to begin): ') 

,这将继续要求他们输入......嗯,任何可能的输入:)


你的问题是,你得到两次输入:

print "Are you ready to begin?" 
answer_1 = raw_input() # <==== Once here 

while answer_1 != "yes" or answer_1 != "y" or answer_1 != "Yes": 
    print "Type 'yes' when you are ready to begin." 
    answer_1 = raw_input() # <==== again here 

然后您的if进入while循环。但你不需要那样做。

它帮助,如果你认为你的程序,并将它写出第一个无需编写代码:

  • 而用户没有提供正确的输入,要求用户输入
  • 然后,请他们为他们的选择

这也就意味着在代码是你将有两个独立的模块:

# Block 1 
# while not_correct_input: 
#  get input 

# Block 2 
# ask for choices 

现在你可以用代码替换这些,你需要:

from __future__ import print_function 

# Block 1 
# while not correct input 
# get input 

# Block 2 
name = raw_input("What is your name? ") 
color = raw_input("What is your favorite color? ") 
home = raw_input("Where do you live? ") 
print("So, you're %r. You're favorite color is %r, and you live in %r" % (name, color, home)) 

一旦你尝试了这一点和它的作品,那么你可以继续添加你的支票正确输入:

# Block 1 
answer_1 = '' 
while answer_1.lower() not in ('y', 'yes', 'si', 'da', 'yes, please'): 
    answer_1 = raw_input('Are you ready to begin? (type "yes" to begin): ') 

# Block 2 
name = raw_input("What is your name? ") 
color = raw_input("What is your favorite color? ") 
home = raw_input("Where do you live? ") 
print("So, you're %r. You're favorite color is %r, and you live in %r" % (name, color, home)) 

现在当所有的工作的,继续前进,添加你希望你的程序的其他部分:

from __future__ import print_function 

print(''' 
Hello, Sir or Madame. My name is Oswald, and I will be asking you a few questions, today. 
'''.strip()) 

# Block 1 
answer_1 = '' 
while answer_1.lower() not in ('y', 'yes', 'si', 'da', 'yes, please'): 
    answer_1 = raw_input('Are you ready to begin? (type "yes" to begin): ') 

# Block 2 
name = raw_input("What is your name? ") 
color = raw_input("What is your favorite color? ") 
home = raw_input("Where do you live? ") 
print("So, you're %r. You're favorite color is %r, and you live in %r" % (name, color, home)) 

这里是什么这个代码看起来像在行动的例子:

Hello, Sir or Madame. My name is Oswald, and I will be asking you a few questions, today. 
Are you ready to begin? 
Are you ready to begin? (type "yes" to begin): no 
Are you ready to begin? (type "yes" to begin): maybe 
Are you ready to begin? (type "yes" to begin): probably 
Are you ready to begin? (type "yes" to begin): okay 
Are you ready to begin? (type "yes" to begin): yes 
What is your name? Wayne 
What is your favorite color? Blue... no yellow! 
Where do you live? Camelot 
So, you're 'Wayne'. You're favorite color is 'Blue... no yellow!', and you live in 'Camelot' 
+0

我试过这个,但我仍然在跳过第一个输入。如果我回答“是”或任何其他列出的“是”的迭代,则当您准备开始时,它仍然会处理“类型”为“是”。串。我在这里做错了什么? –

+0

@DakotaFreeman没有看到你的代码,我猜你已经把原来的第一个“输入”放在那里了。我已经添加了*精确*代码示例,以及示例输入/输出。如果我的最后一个例子不起作用,那么你做了一些可怕的错误。 –

0

你有你的情况倒退。

在Python(以及一般编程)中,如果上述任何项目为真,则or声明为真。

那么,当你说answer != A or answer != B那么,因为答案只能有一个值,结果将永远是真实的。这是因为,如果答案是C,那么它将是双重真实的,而如果答案是A,那么“或答案!= B”部分将是真实的,而如果答案是B,则“答案!= A”部分将是真正。

如果您正在测试的可能可接受的响应列表,您需要使用or要么使它成为一个包容性测试:

if answer = A or answer = B or answer = C ... 

或使其使用and独家测试:

if answer != A and answer != B and answer != C ... 

对于if报表和while报表,这是正确的。

+0

然而,正如我上面评论的那样,感谢您的精彩课程,在while循环部分中更改我的实例或实例会导致整个程序结束。我没有正确地掌握一些东西吗? –

-1

使用and代替while循环中的or

编辑:

单个while循环不足以用于检查二者如果输入是“是”为进入迭代并且如果输入是一个“否”继续迭代,所以增加了一个if进入迭代的声明。

print "Hello, Sir or Madame.", 
print "My name is Oswald, and I will be asking you a few questions, today." 
print "Are you ready to begin?" 
answer_1 = raw_input() 
if (answer_1 == "yes") or (answer_1 == "y") or (answer_1 == "Yes"): 
    answer_1="no" 
    while answer_1 != "yes" and answer_1 != "y" and answer_1 != "Yes": 
     print "Type 'yes' when you are ready to begin." 
     answer_1 = raw_input() 

    if (answer_1 == "yes") or (answer_1 == "y") or (answer_1 == "Yes"): 
     print "What is your name" 
     name = raw_input() 
     print "What is your favorite color?" 
     color = raw_input() 
     print "Where do you live?" 
     home = raw_input() 
     print "So, you're %r. You're favorite color is %r, and you live in %r" % (name, color, home) 
+0

我改变了所有的或者和的实例,但是现在程序只是在第一个raw_input被放置之后结束。还有什么我失踪? –

+0

@DakotaFreeman检查我编辑过的代码。 –

0

好的,我相信我已经得到它的工作方式,你想如何。第一

一件事: 我做你的病情有它做这个计算结果为一个真或假。然后

while not condition: 
print "Type 'yes' when you are ready to begin." 
answer_1 = raw_input() 
condition = ((answer_1 == "yes") or (answer_1 == "y") or (answer_1 == "Yes")) 

,这是重要的部分:

condition = ((answer_1 == "yes") or (answer_1 == "y") or (answer_1 == "Yes")) 

然后开始while循环,你只需要做到这一点。 您需要将问题移出while循环。 因为如果一段时间评估为真,如果你准备好了,它会反复询问你。 但是,如果您在开始时回答yes,while循环的计算结果为False,并且跳过了问题的代码。 如果你想让它反复问的问题只是将它们放在一个

while True: 
#ask the questions 

哦也,你不会,如果你做这种方式需要的if语句。 这是因为如果你在开始时回答否,你将进入while循环,直到你输入yes。之后循环关闭,并提出问题。

我希望能回答你的问题。 保持良好的工作!

0

试着改变你的while声明是这样的:

print "Are you ready to begin?" 
answer_1 = raw_input() 

while answer_1 == "yes" or answer_1 == "y" or answer_1 == "Yes": 
    print "Type 'yes' when you are ready to begin." 
    answer_1 = raw_input() 
    if (answer_1 == "yes") or (answer_1 == "y") or (answer_1 == "Yes"): 
     print "What is your name" 
     name = raw_input()