Using Python 3.10.2, and it won't recognize any integers that I input as an integer. Be it 0, 1, 2.54, -96, or literally any other number, it's just not an integer, apparently.
I'm making a simple guessing game, and my code is as follows:
userGuess = int(input("Guess a number between 1 and 20: "))
if userGuess != int:
print("Unrecognized input! Did you input a number?")
Regardless of the input, it always prints the "unrecognized input" message with the message it's supposed to print for that input ("Your guess was incorrect!", etc.)
If I use the type()
function, it says that userGuess' type is -1. This also only happens when the user inputs the number, not if the number is pre-determined.
CodePudding user response:
If you want to check if the input is not a number, you should validate the input before transforming it into an int
. You can check if a string contains only digits by using .isnumeric
. Something like:
userGuess = input("Guess a number between 1 and 20: ")
if not userGuess.isnumeric():
# Input string is not a number
print("Unrecognized input! Did you input a number?")
else:
# Input is a number, is safe to transform into an int
userGuess = int(userGuess)
This will only be useful if you're considering int
s. If you want to allow float
s (ex: 3.14), maybe you better try to transform the value into a float
and see if it fails:
userGuess = input("Guess a number between 1 and 20: ")
try:
userGuess = float(userGuess)
except ValueError:
# Input string is not a float
print("Unrecognized input! Did you input a number?")
CodePudding user response:
This check:
if userGuess != int:
will always be true because userGuess
will never be the same as the int
type itself. What you're trying to do is check if userGuess
is an instance of an int
:
if isinstance(userGuess, int):
That, however, is not useful here, because this line:
userGuess = int(input("Guess a number between 1 and 20: "))
will either assign an int
to userGuess
, or raise an exception (which will immediately end execution of your script if nothing catches it).
If you want to catch the exception and provide an error, use try/except
:
try:
userGuess = int(input("Guess a number between 1 and 20: "))
except ValueError:
print("Unrecognized input! Did you input a number?")
Note that if your script continues after this point, userGuess
may not have any value assigned to it at all, so you should either end the script immediately since it's impossible to continue (this will probably happen anyway via another exception being raised as soon as you try to use userGuess
), or use a while
loop to re-try the input until userGuess
does have a value.
CodePudding user response:
Sorry, I responded to fast. Here is an updated response. Trying to convert an non-numeric to int will result in a ValueError. However, you can "catch" the error and respond according as follows.
userGuess = input("Guess a number between 1 and 20: ")
try:
userGuess = int(userGuess)
except ValueError:
print("Unrecognized input! Did you input a number?")