Reputation: 99
I'm trying to make a guessing game with the option for the user to quit the program, but I'm unable to get the 'quit' part of the program to work as intended.
from random import randint
userGuess = input("Pick a number from 1 to 1000: ")
guess = randint(1, 1000)
while userGuess != guess:
try:
userGuess = int(userGuess)
if userGuess > guess:
print("The number you entered is higer than the guess. Try again.\n")
userGuess = int(input("Pick a number from 1 to 1000: "))
elif userGuess < guess:
print("The number you entered is lower than the guess. Try again.\n")
userGuess = int(input("Pick a number from 1 to 1000: "))
else:
print("You guessed it!")
break
except (TypeError, ValueError):
userGuess = str(userGuess)
if userGuess.lower() == 'quit':
print("Quitting....")
break
Whenever I run this program, if I type 'quit' when the game begins, it quits the program. However, if I guess a number before I type quit, the program goes through the if statements like it should, but when you try to quit after it just defaults, "the number entered is too low. try again" instead of quitting the program as intended.
I didn't know if there was a better way to handle the 'quit' command other than the try-except method.
Upvotes: 0
Views: 550
Reputation: 74
Edit: In the line
userGuess = int(input("Pick a number from 1 to 1000: "))
When you enter a number in the first place your userGuess is the number you have selected. Then you try to enter 'quit' , but the typecasting integer throws an error because it cannot do int('quit')
. This causes the program to reach the except:
part. However, you did not correctly change the userGuess value because of the error, and it is still the last number you have entered. This is why the if userGuess.lower() == 'quit':
condition never evaluates to True.
A simple fix would be taking the input first and tring to typecast later.
EDIT: As AMC pointed out the following part is wrong. It doesn't take into account that some unicode characters also return True for isnumeric, I was wrong. You can refer to How can I check if a string represents an int, without using try/except? for ways to do this without try/except.
You can use the isnumeric function to check whether the input is an integer or a string.
user_input = input()
if user_input.isnumeric():
#Do something
else:
#Do something else
Upvotes: 0
Reputation: 2702
saint-jaeger beat me to the punch, the issue is indeed that you only ask for input again if the previous input was larger or smaller than the target number. If you enter 'quit' after having previously entered a number at least once, the userGuess
still contains the previous number input.
Your code could be reorganized and simplified, which should make it easier to avoid these kinds of issues:
from random import randint
target = randint(1, 1000)
while True:
user_input = input('Enter your guess (between 1 and 1000), or "quit" to quit: ')
if user_input.casefold() == 'quit':
print('Quitting.')
break
try:
user_guess = int(user_input)
except ValueError:
print(f'Invalid input: {user_input}. Please try again.')
continue
if user_guess > target:
print('Your guess is bigger than the number, try again.')
elif user_guess < target:
print('Your guess is smaller than the number, try again.')
else:
print(f'You guessed correctly, the number was {target}')
break
Notice the amount of code under the try
clause, which is only as big as necessary.
Upvotes: 1