0

I'm trying to code in a condition where if a user doesn't input an integer, it comes up with an error message. I'm using if statements to generate this. How would I do this??

Age=int(input("What is your age in years?")) if Age>20 and Age<150: print("You are an adult") elif Age>20 and Age>=150: print("Not a valid age") elif Age<20: print("still a teenager") 

If someone inputted £ as their age, is it possible for the message to print as "Invalid number. Please try again." ?

I've tried to recode it and it didn't work.

1 Answer 1

1

This is what it should look like:

Age=input("What is your age in years?") try: x = int(Age) if Age>20 and Age<150: print("You are an adult") elif Age>20 and Age>=150: print("Not a valid age") elif Age<20: print("still a teenager") except ValueError: print("Input not valid!") 

It trys to convert the String into a Int if that doesnt work the user didnt input a valid Number.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.