实际上,你可以简化你想要做什么:
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'
的可能的复制[为什么一个变量的非平等对证许多值总是返回true?(http://stackoverflow.com/questions/26337003 /为什么 - 非平等 - 检查 - 的 - 一个变量,对一对多值,总是返回真) – Barmar
您可以用'answer.lower()',以确保输入的是小写不管用户输入什么内容。 – Astrom