簡體   English   中英

條件計數循環Python

[英]Conditional Count Loop Python

我試圖在下面的代碼中找出我的錯誤所在,該代碼與猜測一個單詞有關。

如果輸入缺少的單詞"ghost" ,它應該結束游戲,但仍會繼續要求它。

我的錯誤在哪里?

number = 0
missword = "ghost"
while number < 3:
    guess = input("What is the missing word? ")
    if guess != "ghost":
        print("Try again")
        number = number + 1
    elif guess == missword:
        print("Game Over")
    else:
        print("Game over")

您的所有循環操作都是打印,您需要一個break語句:

number=0
missword="ghost"
while number<3:
    guess = input("What is the missing word? ")
    if guess!="ghost": # Assuming you actually want this to be missword?
        print("Try again")
        number += 1 # Changed to a unary operator, slightly faster.
    elif guess==missword:
        print("Game Over")
        break
    else:
        print("Game over")
        # Is this ever useful?

在滿足退出條件之前, break退出循環。 另一方面, Print只是將文本輸出到stdout ,僅此而已。

暫無
暫無

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

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