while循环在python中没有爆发



我试图建立数字猜谜游戏。我创建了一个game()函数,首先让你选择难度等级,然后根据关卡循环的数量让用户猜测。我现在的问题是如何脱离这个圈子。我创建了一个名为is_continue的布尔值,将其设置为True,并希望在需要跳出循环时使用它(当你猜对了正确的数字,当你想结束游戏时),但它不仅没有跳出循环,而且即使范围数量已经完成,它也会继续重新循环。

from random import randint
from replit import clear
value = randint(1, 100)
def compare(guess, value):
if guess > value:
print("Too high.")
elif guess < value:
print("Too low.")  
else:
print(f"You got it! The answer was {value}")
def game(): 
print("Welcome to the number guessing game.")
print("I am thinking of a number between 1 and 100.")
difficulty_level = input("Choose a difficulty. Type 'easy' or 'hard'.")
is_continue = False
while not is_continue:
if difficulty_level == "easy":
e_attempt = 10
for x in range (10):
print (f"You have {e_attempt} attempts.")
e_attempt -= 1
guess = int(input("Guess a number?"))
compare(guess, value)
if guess == value: 
is_continue = True
if x == 9:
print("You've run out of guess. You lose.")
if input("Type 'c' to continue and 'e' to end.") == "c":
clear()
game()
else:
# I put print function to check weather else statement is working.
print("else statement has been read.")
is_continue = True

elif difficulty_level == "hard":
h_attempt = 5
for x in range (5):
print (f"You have {h_attempt} attempts.")
h_attempt -= 1
guess = int(input("Guess a number?"))
compare(guess, value)
if guess == value:
is_continue = True
if x == 4:         
print("You've run out of guess. You lose.")
if input("Type 'c' to continue and 'e' to end.") == "c":
clear()
game()
else:
clear()
clear()

game()

当你将True赋值给is_continue变量时,它将不会跳出while循环。您可以键入break以跳出当前正在运行的for循环,并且在while循环的下一次迭代时,它将不会执行,因为is_continue变量的值被设置为True

代码示例:

while not is_continue:
if difficulty_level == "easy":
e_attempt = 10
for x in range (10):
print (f"You have {e_attempt} attempts.")
e_attempt -= 1
guess = int(input("Guess a number?"))
compare(guess, value)
if guess == value: 
is_continue = True
break  # this was added

请记住,在这个例子中,在while循环中给变量赋值并不是一件好事。变量difficulty_level在您的代码中永远不会改变。在for循环结束后,它实际上会重新开始。break是跳出当前循环的好方法,特别是在使用嵌套循环时。

我希望我的回答有帮助,祝你编码好运!

您的问题可能是由于您如何使用is_continue布尔变量。当玩家猜测正确的数字时,将代码中的is_continue更改为True,但当玩家决定在猜测结束后继续玩游戏时,也将其设置为True。这可以让循环继续,即使玩家已经用尽了他或她的尝试。

为了解决这个问题,使用break语句在玩家猜出正确的数字或决定结束游戏时立即停止循环。

def game():
print("Welcome to the number guessing game.")
print("I am thinking of a number between 1 and 100.")
difficulty_level = input("Choose a difficulty. Type 'easy' or 'hard'.")
is_continue = True  # Set is_continue to True initially
while is_continue:  # Use while loop with is_continue as the condition
if difficulty_level == "easy":
e_attempt = 10
for x in range(10):
print(f"You have {e_attempt} attempts.")
e_attempt -= 1
guess = int(input("Guess a number?"))
compare(guess, value)
if guess == value:
print("You win!")
is_continue = False  # Set is_continue to False to exit the loop
break  # Exit the loop immediately
if x == 9:
print("You've run out of guesses. You lose.")
if input("Type 'c' to continue and 'e' to end.") == "c":
clear()
game()
else:
is_continue = False  # Set is_continue to False to exit the loop
break  # Exit the loop immediately
elif difficulty_level == "hard":
h_attempt = 5
for x in range(5):
print(f"You have {h_attempt} attempts.")
h_attempt -= 1
guess = int(input("Guess a number?"))
compare(guess, value)
if guess == value:
print("You win!")
is_continue = False  # Set is_continue to False to exit the loop
break  # Exit the loop immediately
if x == 4:
print("You've run out of guesses. You lose.")
if input("Type 'c' to continue and 'e' to end.") == "c":
clear()
game()
else:
is_continue = False  # Set is_continue to False to exit the loop
break  # Exit the loop immediately
clear()
game()

同样,当玩家决定通过输入"e"来结束游戏时,将is_continue设置为False是很重要的,这样循环就会退出,游戏也就结束了。

最新更新