Python:关于while循环的非常基本的帮助

Python:关于while循环的非常基本的帮助,python,while-loop,Python,While Loop,我到处寻找我的问题的答案,但我仍然找不出答案!答案可能很简单,但我就是不明白,可能是因为我刚刚回到Python 无论如何,我想创建一个while循环,这样在用户输入“y”或“n”之前,问题将一直被询问。这就是我所拥有的: while True: # to loop the question answer = input("Do you like burgers? ").lower() if answer == "y" or "n": break 老实说,我

我到处寻找我的问题的答案,但我仍然找不出答案!答案可能很简单,但我就是不明白,可能是因为我刚刚回到Python

无论如何,我想创建一个while循环,这样在用户输入“y”或“n”之前,问题将一直被询问。这就是我所拥有的:

while True:   # to loop the question
    answer = input("Do you like burgers? ").lower()
    if answer == "y" or "n":
        break 

老实说,我太受宠若惊了,所以我请求别人帮助:)

你的情况不对。另外,如果您使用的是Python2.x,您应该使用
raw\u input
(否则您需要键入
“y”
,而不是
y
,例如):


if answer in(“y”,“n”):
如果您想知道为什么会这样做:在Python中,字符串的真值为true。非空字符串总是true:
bool(“foo”)==true
,因此您的代码总是会因为
而中断。。。或对应于
的“n”
。。。或为真
while True:   # to loop the question
    answer = input("Do you like burgers? ").lower()
    if answer == "y" or answer == "n":
        break 
while True:   # to loop the question
    answer = raw_input("Do you like burgers? ").lower()
    if answer == "y" or answer == "n":
        break