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

Edw*_*ric 3 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 
Run Code Online (Sandbox Code Playgroud)

说实话,我真是太困惑了,所以我求求别人的帮助:)

Ste*_*enG 5

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

  • 如果你想知道为什么会这样:在Python中,字符串的真值是真的.一个非空字符串总是如此:`bool("foo")== True`,因此你的代码总是会因为`...或'n"`对应于`...或True`而中断. (2认同)