Search code examples
python

How to make Python check whether a variable is a number or letter


How do I make my code check whether input from a user is a number or a letter?

age = input()

if age == int or float:
    print("Ok, zavrsili smo sa osnovnim informacijama! Da li zelite da ih uklopimo i pokazemo Vase osnovne informacije? DA ili NE ?")

elif age == False:
    print("Hej, to nije broj... Pokusaj ponovo")

This is part of my code that I'm having issues with. If the user inputs his age as number, I want the code to continue. But, if the user inputs something that is not a number, the code should tell them to start over.


Solution

  • Assuming that you are getting the value 'age' from the user via an input(..) call then to check if the age is a number then:

    age = input('Provide age >')
    if age.isnumeric():
        age = int(age)
        print("Ok, zavrsili smo sa osnovnim informacijama! Da li zelite da ih uklopimo i pokazemo Vase osnovne informacije? DA ili NE ?")
    else:
         print("Hej, to nije broj... Pokusaj ponovo")