我正在用 python 开发一个猜谜游戏,它几乎完成了。但错误在于,即使我已将初始变量重新分配给新用户输入,它也会从第一个用户输入开始检查条件。请问我该怎么做才能解决它?
import random
number = random.randint(1,101)
print(f"the no is {number}")
def check(user_guess):
global level
global game_over
if level <= 0:
print("you ran out of guesses...")
return False
if user_guess > number:
print("too high")
level -= 1
print(f"guesses left: {level}")
user_guess = int(input("guess again "))
game_over = False
elif user_guess < number:
print("too low")
level -= 1
print(f"guesses left: {level}")
user_guess = int(input("guess again "))
elif user_guess == number:
print("you guessed right")
return False
level = 0
def levels():
'''assigns the level to the user'''
global game_over
global level
print("Welcome to the number guessing game...")
difficulty = input("enter a difficulty: Easy or Hard? ").lower()
if difficulty == 'easy':
level = 10
print(f"guesses: {level}")
elif difficulty == 'hard':
level = 5
print(f"guesses : {level}")
else:
print("error")
game_over = False
levels()
user_guess = int(input("make a guess "))
while not game_over:
check(user_guess)
if check(user_guess) == False:
game_over = True
这是完整的代码 我试过在不同的地方调用检查功能
问题出在这段代码上:
user_guess = int(input("make a guess "))
while not game_over:
check(user_guess)
if check(user_guess) == False:
game_over = True
您的代码要求输入,然后将其设为 int,并将其存储在变量 user_guess 中。然后,您使用检查功能检查变量。但是,您不会再次要求输入!
这样想。如果我输入
3
,user_guess变量被设置为数字3并且再也没有改变过。
要解决此问题,请将
user_guess = int(input("make a guess "))
位放在 check() 函数之前。
while not game_over:
user_guess = int(input("make a guess "))
check(user_guess)
if check(user_guess) == False:
game_over = True
您应该在 while 循环中获取用户输入。
while not game_over:
user_guess = int(input("make a guess "))
check(user_guess)
if check(user_guess) == False:
game_over = True
我认为您的代码的问题在于您没有在 while 循环内更新用户猜测。您每次都将相同的用户猜测传递给检查功能,即使在用户再次猜测之后也是如此。
如果我可以建议一个解决方案是尝试将用户猜测的行移动到 while 循环中,以便它在每次迭代时更新。
嗨,我刚刚为您重新制作了它,我认为它更简单,但希望这对您有所帮助
import random
import sys
number = random.randint(1, 100)
print(number)
def hard():
print("Enter a numbr between 1 and 100")
guesses = 5
while True:
print(f"You have {guesses} guesses left")
guess = int(input("Enter a number : "))
if guesses == 0:
again = input("game over!\nTry again ? ").upper()
if 'YES' in again:
level()
else:
print("goodbye")
break
elif guess > 100 or guess < 0:
print("Please only numbers between 1 and 100")
elif guess < number:
print("guess is too low try again")
guesses = guesses - 1
elif guess > number:
print("guess is too high try again")
guesses = guesses - 1
else:
again = input("Well done correct\nTry again ? ").upper()
if 'YES' in again:
level()
else:
print("goodbye")
break
def level():
print("""
Enter a level Easy or Hard
""")
while True:
chosen_level = input(">").upper()
if chosen_level == "HARD":
hard()
elif chosen_level == "EASY":
easy()
else:
print("Invalid option")
def easy():
print("Enter a numbr between 1 and 100")
guesses = 10
while True:
print(f"You have {guesses} guesses left")
guesses = int(input("Enter a number : "))
if guess == 0:
again = input("game over!\nTry again ? ").upper()
if 'YES' in again:
level()
else:
print("goodbye")
break
elif guess > 100 or guess < 0:
print("Please only numbers between 1 and 100")
elif guess < number:
print("guess is too low try again")
guesses = guesses - 1
elif guess > number:
print("guess is too high try again")
guesses = guesses - 1
else:
again = input("Well done correct\nTry again ? ").upper()
if again == "YES":
level()
else:
print("goodbye")
break
level()
你的代码有多个问题,包括要求用户输入两次,一次在外面
check()
一次在里面 check()
.
更不用说,您的代码非常难读,这使得找出问题变得更加困难。
while not game_over:
# Here is your problem. You are checking the users guess and then checking their new one.
# However the value of user_guess is not being changed
check(user_guess)
print(user_guess)
if check(user_guess) == False:
game_over = True
我跑了一个例子,其中数字是
22
。我先猜21
,它说太低了。当它让我再猜的时候,我说23
。之后,打印语句运行;但是,它打印了21
。这表明user_guess
没有被更新。
下面,我已经修复并改进了关于“错误”和可读性的代码
import random
def check(user_guess, guesses):
'''Checks if the users guess was correct.
Otherwise, gives feedback on whether it was too high or too low'''
if guesses <= 1:
print("you ran out of guesses...")
exit(0)
if user_guess > number:
print("too high")
guesses -= 1
print(f"guesses left: {guesses}")
elif user_guess < number:
print("too low")
guesses -= 1
print(f"guesses left: {guesses}")
elif user_guess == number:
print("you guessed right")
exit(0)
return guesses
def get_input(text: str):
'''Gets input from the user, and attempts to convert it to an integer.
Exits the program if it fails.'''
try:
return int(input(text))
except ValueError:
print("Invalid number")
exit(0)
def levels():
'''assigns the level to the user.
Returns the amount of guesses the user gets based on the level they chose.'''
print("Welcome to the number guessing game...")
difficulty = input("enter a difficulty: Easy or Hard? ").lower()
if difficulty == 'easy':
guesses = 10
print(f"guesses: {guesses}")
elif difficulty == 'hard':
guesses = 5
print(f"guesses : {guesses}")
else:
print("error")
exit(0)
return guesses
if __name__ == '__main__': # Easy way to tell the reader where to start reading the program
number = random.randint(1,101)
guesses = levels()
print(f"the no is {number}")
while True:
guesses = check(get_input("Make a guess: "), guesses)