Respuesta :

If you would like to check the value on an input, you will have to handle the ValueError that may occur if you try to convert the input.

- If you want the input to be a string, then that's simple, because input is already a string :)

- If you want the input to be an integer, a float, or whatever else, of course you would have to convert your string input to whatever you require. For example:

If you try
age = int(input('Enter your age: '))
And the user enters something like 5, then that's fine and will convert no problem.
However, if you tried to enter 1.5, or 'hello' or something that is NOT an integer, you will notice it generates something called a "ValueError", and the program will cease to parse.
This is what I mean by handling the error. We make our program 'handle' the error and do something else if it occurs, so that it doesn't crash our program and it can continue parsing.
This can be done by using the keywords "try" and "except"

try is a block of code that you expect an error (called an exception in other languages, hence the keyword "except"), and the except catches certain type of errors. In the exam below, we have a loop that continuously asks the user for their age until the value is valid:

age = None
while not isinstance(age, int):
    try:
        age = int(input('Enter your age: '))
        if age < 0:
            raise ValueError
    except ValueError:
        print('That is not a valid age.')

Let me know if you have any questions :)