i was working on a guessing game in python and its almost complete. but the bug is that it keeps checkng the condition from the first user input even though I've reassigned the initial variable to a new user input. pls what can i do to fix it?
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
this is the entire code ive tried calling the check function in different places
The problem is with this chunk of code:
user_guess = int(input("make a guess "))
while not game_over:
check(user_guess)
if check(user_guess) == False:
game_over = True
Your code asks for an input, then makes it an int, storing it in the variable user_guess. Then, you use your check function to check the variable. However, you do not ask for an input again!
Think about it this way. If I input 3
, the user_guess variable is set as the number 3 and never changed again.
To fix this issue, put the user_guess = int(input("make a guess "))
bit right before the check() function.
while not game_over:
user_guess = int(input("make a guess "))
check(user_guess)
if check(user_guess) == False:
game_over = True