【问题标题】:Break a loop and quit the program when user types ''quit'' in Python当用户在 Python 中键入 ''quit'' 时,中断循环并退出程序
【发布时间】:2021-03-26 15:44:21
【问题描述】:

我有这个用 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 中删除被调用的数字,直到列表为空。

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

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

【问题讨论】:

  • 您正在接收整数作为输入。您是否希望用户输入整数或 quit
  • 我编辑了您的问题,以便它直接询问您要提出的问题并避免跑题。请注意差异,以便您以后可以提出更有效的 Stack Overflow 问题。
  • 无论如何,你应该向我们展示你尝试过的没有工作,而不仅仅是你迄今为止工作的部分。你告诉我们你不知道如何使用break,但大概你试图以某种方式/在哪里使用break。你到底尝试了什么?当你尝试这样做时发生了什么?如果您遇到错误,您应该尝试理解错误,并显示错误消息。您还应该更准确地解释您希望它如何工作。例如,用户是否会键入quit * 以响应Please enter the announced Bingo Number: 问题?还是分开问?
  • 谢谢@KarlKnechtel,我非常感谢您的编辑版本,它让一切变得更简单,并且更好地理解了我的需求。毫无疑问,我将在下一个问题中遵循您的建议,揭露我的错误并尝试更好地解释我希望它如何工作。惊人!谢谢!

标签: python


【解决方案1】:

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

还请注意,您不想只调用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!!!")

【讨论】:

  • 您可以删除 if 语句并使用 while ... else 只有当 while 语句为 false 时才会执行 else。如果您跳出 while 语句,它将不会执行。为确保打印 Okay bye!,请在 break 之前添加 print 语句
  • 请不要只给出完整的代码而不做解释。你应该谈谈你需要改变的事情,以及为什么。
  • @JoeFerndz 好建议!我不知道存在while ... else 语法。非常感谢---我会编辑答案。 @KarlKnechtel 确实是好点,我尝试先提交答案,然后编辑答案以给出解释。但感谢您支持生产环境。
  • 因为你要检查小写的quit,所以一定要给user_input.lower() == 'quit'
  • 这里有更多关于while else的信息...stackoverflow.com/questions/3295938/…
【解决方案2】:

首先列出一个退出列表,并在适当的位置放置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!!!")

【讨论】:

  • 我真的很喜欢制作“退出”列表的想法,让用户有更多的打字选择,但退出后,程序仍然显示 BINGO!最后,这个问题也在上面的答案中得到了解决。感谢您的时间和解决方案@Lostman
【解决方案3】:

可以试试这个:

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。请注意,另一种跳出循环的方法是当您猜出卡片上的所有数字时。

【讨论】:

  • 您可以将while语句设为while ch.lower() != 'q' or len(bingoCard) == 0。这样你就不需要在里面有另一个 if 语句
  • 不是最有效的方法
  • @JoeFerndz 在这种情况下,即使列表的长度为 0,控件也会进入循环。您的意思是 while ch.lower() != 'q' and len(bingoCard) != 0?
  • 我在里面添加了另一个 if 只是为了确保程序不会询问用户是要继续还是退出,以防万一他在该迭代中获得 BINGO。
  • 哎呀。它应该是 and 而不是 or 。更新后的 while 语句应为 while ch.lower() != 'q' or len(bingoCard) != 0
猜你喜欢
  • 1970-01-01
  • 2018-07-25
  • 1970-01-01
  • 2016-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多