0

How do you restrict the user to only inputting an integer to make a code work?

I created a code where the user inputs their first_name, last_name, and user_age. If the user inputs anything other than an integer into the "user_age", i want a message to pront stating that "That is not a valid age." Ive googled ans searched and tried and cant seem to get the code to work for it. Can anyone help me out? I need some ideas and/or explanations. The code file is public and called "Century Age".

11th Dec 2016, 5:48 AM
Jay Adim
Jay Adim - avatar
4 Réponses
+ 2
If you are getting age from user then use user_age = int(raw_input("enter your age:- ")) OR user_age = float(raw_input("Enter your age:- "))
11th Dec 2016, 6:13 AM
Musawer Shah
Musawer Shah - avatar
+ 1
try : r=int(input()) except ValueError: print ("please enter an integer")
11th Dec 2016, 11:59 AM
nitesh
0
Yes im already using user_age = int(input("..")) But if they decide to put in a letter instead, i want to make a response, instead of the code just giving me an error
11th Dec 2016, 6:16 AM
Jay Adim
Jay Adim - avatar
0
Use a try catch and except. SoloLearn has it along it's syllabus lines.
11th Dec 2016, 7:20 AM
JENN
JENN - avatar