簡體   English   中英

while循環應停止時重復一次

[英]while loop repeats once when it should stop

當option =“ e”時,此while循環應停止,但是當option =“ e”時,它將以某種方式將option設置為“”(我已經通過調試器進行了檢查)並再次運行(盡管option =“”)不在while循環,僅當您將選項設置為等於“ e”兩次時,它才會停止。 Option不是全局變量,因此功能無法更改其值。

我已經嘗試過使用while True:和option =“ e”時的break命令,但是它不會脫離循環

option = ""
hand = {0:0}
while option != "e":
    option = input("Enter n to deal a new hand, r to replay the last hand, or e to end game: ")
    if option == 'n':
        hand = dealHand(HAND_SIZE)
        playHand(hand,wordList,HAND_SIZE)
    elif option == 'r':
        if 0 in hand:
            print("You have not played a hand yet. Please play a new hand first!","\n")
        else:
            playHand(hand,wordList,HAND_SIZE)
    elif option != "e":
        print("Invalid command.")

如果要求我“輸入n來發新手,r播放最后一手,或e結束游戲”,當我鍵入“ e”(不帶引號)時,我希望它會跳出循環。

檢查這是否是您所期望的。 我試圖重新創建上述錯誤,但無法重新創建。 讓我知道您是否再得到它。

option = ""
hand = {0:0}
while True:
    option = input("Enter n to deal a new hand, r to replay the last hand, or e to end game: ")
    if option == 'n':
        hand = dealHand(HAND_SIZE)
        playHand(hand,wordList,HAND_SIZE)
    elif option == 'r':
        if 0 in hand:
            print("You have not played a hand yet. Please play a new hand first!","\n")
        else:
            playHand(hand,wordList,HAND_SIZE)
    elif option == "e":
        break
    elif option != "e":
        print("Invalid command.")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM