- 2
Why the heck is this not working??
user_age = input() if user_age != int: print("Sorry, that is invalid.") I'm using Pydroid and when I run the above code it returns Sorry, that is invalid every stinking time. I could not find anything helpful on the Internet.
8 Réponses
+ 5
Try this:
if user_age.isnumeric():
print("...")
+ 4
--More about isnumeric() method--
The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False.
Exponents, like ² and ¾ are also considered to be numeric values.
"-1" and "1.5" are NOT considered numeric values, because all the characters in the string must be numeric, and the - and the . are not.
Syntax-
string.isnumeric()
+ 3
Take int as input
user_age = int(input())
+ 2
6hpxq9 Yeah I got it, thanks :) Slowly learning 😅
+ 1
Thanks! :)
+ 1
Dea Mer Aside from other answers, You can also use the try-except statements to catch error in case the input cannot be converted to int (i.e. not an integer).
try:
user_age = int(input())
except:
print("Sorry, this is invalid")
+ 1
Dea Mer You haven't assigned a value for variable int so all the time it say "Sorry, that is invalid"
+ 1
You have to convert the input to int
I mean, if the var int is an integer :v
user_age = int(input())
It's a common error, don't worry ;)