Why the heck is this not working?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 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.

19th May 2021, 8:27 AM
Dea Mer
8 Answers
+ 5
Try this: if user_age.isnumeric(): print("...")
19th May 2021, 8:33 AM
Gašper Koželj
Gašper Koželj - avatar
+ 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()
20th May 2021, 4:18 AM
Sagar Yadav
Sagar Yadav - avatar
+ 3
Take int as input user_age = int(input())
20th May 2021, 6:18 AM
Hamza Malik
Hamza Malik - avatar
+ 2
6hpxq9 Yeah I got it, thanks :) Slowly learning 😅
21st May 2021, 6:20 AM
Dea Mer
+ 1
Thanks! :)
19th May 2021, 4:38 PM
Dea Mer
+ 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")
20th May 2021, 6:29 AM
noteve
noteve - avatar
+ 1
Dea Mer You haven't assigned a value for variable int so all the time it say "Sorry, that is invalid"
21st May 2021, 3:10 AM
Shobhit :)
Shobhit :) - avatar
+ 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 ;)
21st May 2021, 3:25 AM
6hpxq9
6hpxq9 - avatar