So when you ask the user for input, you need it to give you a valid input, something you can do whatever it is you wanted to do with it. For example, let’s say you ask for the user’s age because you wanna add something to it. Well, the user might write bdjsfkfs, you never know! So you must prepare accordingly.

First of all, be sure to specify when asking the user for input. Do not assume they know what you want from them. Have the program say things like “only integers, or rounded to the nearest whole number or in military time please.”

pink sweet minions despicable me agnes
source

Now, when asking for input, it will always be given as a string, and you won’t be able to do math operations with it.

capture

One thing you must do is transform the user input into a number by using the int function. That way Python will automatically turn it into an integer and will be able to do math operations with it. Example:

age = int(input("Please enter your age: "))

Another great thing you can use in Python is the try/except statement, like this:

def valid_user_input(x):
    try:          #what you want it to do
        return int(x) > 2
    except ValueError:     #if an error happens,
        print ("please type an integer")     #do this
        return None