Python:while循环的基本帮助

2024-09-27 09:36:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我到处找我的问题的答案,但还是找不出来!答案可能很简单,但我就是搞不懂,可能是因为我刚刚回到Python。。。你知道吗

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

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

我真的很受宠若惊,所以我请求别人的帮助:)


Tags: theto答案用户answerloopyoutrue
2条回答

你的情况不对。另外,如果您使用的是Python2.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
while True:   # to loop the question
    answer = input("Do you like burgers? ").lower()
    if answer == "y" or answer == "n":
        break 

相关问题 更多 >

    热门问题