簡體   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