2015-09-06 66 views
3

我到处搜索了一个对我的问题的答案,我仍然无法弄清楚!答案很可能是SOOOOO简单,但我只是不能得到它,也许是因为我刚开始回到Python的...Python:非常基本的帮助while while循环

不管怎样,我想,这样直到用户输入“Y创建一个while循环'或'n'这个问题会不断被问到。这是我有:

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

我老老实实这么baffed,所以我乞求别人的帮助:)

+1

'如果答案( “Y”, “N”):' –

回答

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

如果你想知道为什么这会起作用:在Python中,字符串具有真值。一个非空字符串总是为真:'bool(“foo”)== True',所以你的代码总是会因为......或者n“'对应于'...或True'而中断。 – jojonas

3

你的条件是错误的。此外,如果你正在使用Python 2.x的,你应该使用raw_input(否则你将需要输入"y",而不是y为例):

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

不在python 3.x中... –