我试图找出我的错误在下面的代码中,这是关于猜一个字.
如果我输入缺失的单词,"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")
Run Code Online (Sandbox Code Playgroud)
您的所有循环都是打印,您需要一个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?
Run Code Online (Sandbox Code Playgroud)
break在退出条件满足之前退出循环.Print另一方面,只是输出文本stdout,没有别的.