So I have been watching various videos online with regards to a python3 guessing game, but I can't get it to work. I think the logic is sound, but it just won't work as intended. I keep getting the same error. For example, if the number_guess is 90 and I type in 100 for the user_guess, then I receive a message telling me the number is too high, as intended. But if I then enter a follow up number of 8 for example, it will still tell me the user_guess is too high. Similarly, if I entered 90 which is the number_guess, I still receive an error that the number is too high.
here's the code
number_guess = int(input("Enter the integer for the player to guess:"))
user_guess = int(input("Enter your guess."))
count = 1
while user_guess != number_guess:
count = 1
if user_guess == number_guess:
print("You guessed it in", count, "tries")
elif user_guess < number_guess:
int(input("Too low - try again:"))
elif user_guess > number_guess:
int(input("Too high - try again:"))
CodePudding user response:
On line 9 and 11, you're not saving the new guess into the variable user_guess(the variable that you're checking).
so for every while loop it will keep checking against the same number over and over again.
solution:
user_guess = int(input("Too low - try again:"))
Another thing to look at is your while function. It runs while a certain condition is met. It runs when user_guess is not equal to number_guess. Becuase of this, if you guess the number corrrectly, the line saying "you guessed it in x tries" will never execute.
CodePudding user response:
Hi thank you for your response! I actually modified my code in the meantime and it now looks as:
number_guess = int(input("Enter the integer for the player to guess:"))
user_guess = int(input("Enter your guess."))
count = 0
while user_guess != number_guess:
count = 1
if user_guess == number_guess:
print("You guessed it in", count, "tries")
elif user_guess < number_guess:
print("Too low - try again:")
user_guess = int(input())
elif user_guess > number_guess:
print("Too high - try again:")
user_guess = int(input())
I'm trying to guess the print statement to work now. Wish me luck but thank you so much for your response!
CodePudding user response:
Here's my final code. I had to make some edits for formatting. Thank you again!
print("Enter the integer for the player to guess.")
number_guess = int(input())
print("Enter your guess.")
user_guess = int(input())
count = 1
while user_guess != number_guess:
count = 1
if user_guess < number_guess:
print("Too low - try again:")
user_guess = int(input())
elif user_guess > number_guess:
print("Too high - try again:")
user_guess = int(input())
if user_guess == number_guess:
print("You guessed it in", count, "tries.")