[英]Why doesn't this program end? The game goes on forever if i don't input the correct number
I am new to code and I was trying to create a guessing game as one of my first projects.我是编码新手,我试图创建一个猜谜游戏作为我的第一个项目之一。 I think i'm nearly done but this program doesn't end.
我想我快完成了,但这个程序并没有结束。 Instead of ending when the there are 0 tries left it just continues the game even with the minus, Ps.(I made the number fixed for now, but will generate a random number when this issue is resolved)
而不是在剩下 0 次尝试时结束,它只是继续游戏,即使是减号,Ps。(我现在固定了这个数字,但是当这个问题解决后会生成一个随机数)
I am new to code and I was trying to create a guessing game as one of my first projects.我是编码新手,我试图创建一个猜谜游戏作为我的第一个项目之一。 I think i'm nearly done but this program doesn't end.
我想我快完成了,但这个程序并没有结束。 Instead of ending when the there are 0 tries left it just continues the game even with the minus, Ps.(I made the number fixed for now, but will generate a random number when this issue is resolved)
而不是在剩下 0 次尝试时结束,它只是继续游戏,即使是减号,Ps。(我现在固定了这个数字,但是当这个问题解决后会生成一个随机数)
print("Welcome to my guessing game can you get the magic number hint, it's between 1 and 100 ")
Magic_number = 7
guess = int(input("Enter your guess:"))
guess_limit = 5
guess_counter = 1
out_of_guesses = False
print("You have", str(guess_limit - guess_counter), "tries left")
while guess != Magic_number and not out_of_guesses:
while guess < Magic_number:
print("That number is too small, try again")
guess = int(input("Enter guess: "))
guess_counter += 1
print("You have", str(guess_limit - guess_counter), "tries left")
while guess > Magic_number:
print("That number is too high try again")
guess = int(input("Enter guess: "))
guess_counter += 1
print("You have", str(guess_limit - guess_counter), "tries left")
if guess == Magic_number:
print("Well done you got it!!")
if out_of_guesses:
print("Game over, sorry")
You dont have to do while guess > Magic_number:
or while guess > Magic_number:
.你不必做
while guess > Magic_number:
或while guess > Magic_number:
。 You can directly use if while guess > Magic_number:
and if guess > Magic_number:
instead of them respectively and the program will still keep looping since there's an outer loop.您可以直接使用
if while guess > Magic_number:
和if guess > Magic_number:
分别代替它们,并且由于存在外循环,程序仍将继续循环。
Your program isn't stopping because you have not checked the condition inside the inner while loops and therefore it keeps looping.您的程序没有停止,因为您没有检查内部 while 循环内的条件,因此它一直在循环。 The
guess_counter
will keep increasing inside the inner while loops but you have made no condition to break the loop on basis of guess_counter
. guess_counter
将在内部 while 循环内不断增加,但您没有条件根据guess_counter
打破循环。
Just replace the inner while loops with if since you don't need them anyway.只需用 if 替换内部 while 循环,因为无论如何您都不需要它们。 Hope this solves your problem!
希望这能解决您的问题!
You did not update out_of_guesses
variable, and you may use if
and not while
for the inner loops to avoid multiplying the place to check out_of_guesses
.您没有更新
out_of_guesses
变量,您可以在内部循环中使用if
和 not while
以避免将检查out_of_guesses
的位置相乘。 Then use a break
if finds, that allows to put the code to run agin after it and not duplicate it然后使用
break
if finds,这允许代码在它之后运行而不是重复它
while not out_of_guesses:
if guess < Magic_number:
print("That number is too small, try again")
elif guess > Magic_number:
print("That number is too high try again")
elif guess == Magic_number:
print("Well done you got it!!")
break
guess = int(input("Enter guess: "))
guess_counter += 1
print("You have", str(guess_limit - guess_counter), "tries left")
out_of_guesses = guess_counter>=guess_limit
You are not updating out_of_guesses
wich means that your loop exit clause while not out_of_guesses
is never triggered.您没有更新
out_of_guesses
,这意味着您的循环退出子句while not out_of_guesses
永远不会被触发。
You are looping too much as well;你也循环太多了; your program is getting stuck in the inner while loops.
你的程序陷入了内部的while循环。
Try something like this:尝试这样的事情:
print("Welcome to my guessing game can you get the magic number hint, it's between 1 and 100 ")
Magic_number = 7
guess_limit = 5
guess_counter = 0
out_of_guesses = False
print("You have", str(guess_limit - guess_counter), "tries left")
while not out_of_guesses:
guess = int(input("Enter guess: "))
if guess == Magic_number:
print("Well done you got it!!")
break
elif guess < Magic_number:
print("That number is too small, try again")
elif guess > Magic_number:
print("That number is too high try again")
guess_counter += 1
print("You have", str(guess_limit - guess_counter), "tries left")
# exit clause
if guess_limit == guess_counter:
out_of_guesses = True
print("Game over, sorry")
Few mistakes:几个错误:
The while
loops inside the main loop should be if
statements.主循环内的
while
循环应该是if
语句。
The out_of_guesses
was completely unused in your code out_of_guesses
在您的代码中完全未使用
Tip:小费:
while
(or for
) loop using break
break
退出一段while
(或for
)循环print("Welcome to my guessing game can you get the magic number hint, it's between 1 and 100 ")
Magic_number = 7
guess_limit = 5
guess_counter = 1
guess = None
while guess != Magic_number:
guesses_left = guess_limit - guess_counter + 1
if guesses_left == 0:
print("Game over, sorry")
break
print("You have", guesses_left, "tries left")
guess = int(input("Enter guess: "))
guess_counter += 1
if guess < Magic_number:
print("That number is too small, try again")
elif guess > Magic_number:
print("That number is too high try again")
elif guess == Magic_number:
print("Well done you got it!!")
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.