[英]Conditional Count Loop Python
I'm trying to find out where my mistake is in the following code which is about guessing a word. 我试图在下面的代码中找出我的错误所在,该代码与猜测一个单词有关。
If I type in the missing word, "ghost"
, it should end the game but it continues to ask for it. 如果输入缺少的单词
"ghost"
,它应该结束游戏,但仍会继续要求它。
Where is my mistake? 我的错误在哪里?
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")
All your loop does is print, you need a break statement: 您的所有循环操作都是打印,您需要一个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
exits a loop before the exit condition has been satisfied. 在满足退出条件之前,
break
退出循环。 Print
on the other hand simply outputs text to stdout
, nothing else. 另一方面,
Print
只是将文本输出到stdout
,仅此而已。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.