0

Hello I am creating a registration program and need to ask the user to input their age . However I want to make sure its not a letter by just consisting of numbers. How do I limit the user to only getting a number and if they input other character a error message shows up

    while True:
    age = int(input("Age: "))
    if not (age) != int:
        print ("Not a valid age")
        continue
    else:
        break
  • 3
    Possible duplicate of [How to check if string input is a number?](https://stackoverflow.com/questions/5424716/how-to-check-if-string-input-is-a-number) – Vadim Nov 12 '17 at 13:31
  • Possible duplicate of [Check if a number is int or float](https://stackoverflow.com/questions/4541155/check-if-a-number-is-int-or-float) – Th. Thielemann Nov 12 '17 at 13:31

1 Answers1

0

You can use try and except statements here.

try:
    age=int(age) #Do not typecast the age variable before this line
except ValueError:
    print("Enter number")

If you do not want the program to proceed until the user enters a number, you can use a flag variable and put the code block mentioned above in a while loop.