繁体   English   中英

当用户在 Python 中键入“退出”时,中断循环并退出程序

[英]Break a loop and quit the program when user types ''quit'' in Python

我在 Python 中有一个类似宾果游戏的工作代码(匹配全卡时宣布获胜者):

bingoCard = [7, 26, 40, 58, 73, 14, 22, 34, 55, 68]

while len(bingoCard) != 0:
    nNumberCalled = int(input("\nPlease enter the announced Bingo Number: "))
    if nNumberCalled <1 or nNumberCalled > 80:
        print("Oops, the number should be between 1 and 80.")
    elif nNumberCalled in bingoCard:
        bingoCard.remove(nNumberCalled)
        print(f"Nice on1e! You hit {nNumberCalled}.")
    else:
        print("Nah... Not in your card.")

print("\nBINGO!!!")

这个想法是我从bingoCard中删除被调用的数字,直到列表为空。

我想通过键入“退出”随时为用户提供退出游戏(跳出循环)的选项。

我试图研究这个问题,但我无法弄清楚如何或在何处将break语句添加到我的代码中以使其正常工作。 我想我必须在while循环中包含其他内容,例如try / except或者for循环。 我该如何进行这项工作?

如果字符串quit ,接收输入,然后从while循环中跳出怎么样? 如果输入不是quit ,则照常进行,即将输入解析为 integer。

另请注意,您不想只调用break ,因为即使他/她退出,这也会让用户看到“BINGO”消息。 为了解决这个问题,根据@JoeFerndz 的建议,使用了while... else子句。 这个条款是我不知道的,我认为它非常有用。 谢谢你的问题(当然还有@JoeFerndz 的评论),我可以从中学到新的东西!

bingoCard = [7, 26, 40, 58, 73, 14, 22, 34, 55, 68]

while len(bingoCard) != 0:
    user_input = input("\nPlease enter the announced Bingo Number (or 'quit'): ")
    if user_input.lower() == 'quit':
        print("Okay bye!")
        break
    nNumberCalled = int(user_input)
    if nNumberCalled <1 or nNumberCalled > 80:
        print("Oops, the number should be between 1 and 80.")
    elif nNumberCalled in bingoCard:
        bingoCard.remove(nNumberCalled)
        print(f"Nice on1e! You hit {nNumberCalled}.")
    else:
        print("Nah... Not in your card.")
else:
    print("\nBINGO!!!")

首先列出一个退出列表,然后在适当的地方break一下,如下所示:

bingoCard = [7, 26, 40, 58, 73, 14, 22, 34, 55, 68]

while len(bingoCard) != 0:
    new_var = input("\nPlease enter the announced Bingo Number: ")
    if(new_var in ['quit', 'Quit', 'QUIT']):
        break
    else:
        nNumberCalled = int(new_var)
        if nNumberCalled <1 or nNumberCalled > 80:
            print("Oops, the number should be between 1 and 80.")
        elif nNumberCalled in bingoCard:
            bingoCard.remove(nNumberCalled)
            print(f"Nice on1e! You hit {nNumberCalled}.")
        else:
            print("Nah... Not in your card.")

print("\nBINGO!!!")

可以试试这个:

bingoCard = [7, 26, 40, 58, 73, 14, 22, 34, 55, 68]
ch=''
while ch!='q':
    nNumberCalled = int(input("Please enter the announced Bingo Number: "))
    if nNumberCalled <1 or nNumberCalled > 80:
        print("Oops, the number should be between 1 and 80.")
    elif nNumberCalled in bingoCard:
        bingoCard.remove(nNumberCalled)
        print("Nice on1e! You hit ",nNumberCalled)
    else:
        print("Nah... Not in your card.")
    if len(bingoCard) == 0:
        break
    ch = input("Press q to quit or any other key to continue: ")

if(ch=='q'):
    print("Thank You")
else:
    print("\nBINGO!!!")

我正在做的是,保留一个变量以在每次迭代中获得用户的选择。 如果用户输入'q',代码会跳出循环并检查最后的用户输入,否则游戏继续。 在循环外,如果发现用户最后选择的是'q',则表示用户退出游戏,否则打印BINGO。 请注意,另一种跳出循环的方法是当您猜到卡上的所有数字时。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM