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".
4 Respuestas
+ 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:- "))
+ 1
try :
r=int(input())
except ValueError:
print ("please enter an integer")
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
0
Use a try catch and except.
SoloLearn has it along it's syllabus lines.