The users that run computer programs may not follow the program instructions. Whenever the program asks for a specific kind of input - such as a number or alphabetic string - the user may actually type in something completely different!

Sometimes these mistakes are accidental, and a user has simply typed too fast or not read or understood the instructions. In other cases, a user might deliberately try to "break" your program by doing something unexpected. Either way, a high-quality program will carefully verify user input to make sure it meets the requirements. If a program tries to process invalid input, it might crash with an exception or produce incorrect results.

Risky Numeric Conversions
All user input is read from the console with input(), which returns a string value. The value may contain an integer like "42" or floating point value like "3.14". if your program needs to convert the string to an integer or float data type, you can use the int() and float() functions on that string data. You are familiar with int() and float() from earlier lessons.

The examples below pass the string result from the input() function directly into the int() or float() functions, which will return an integer or floating point result to the variables.

intAnswer = int(input("In what year were you born? "))
floatAnswer = float(input("How much do you want to spend on a milkshake? "))
Copy
This works quite well when the user types in a string that contains a valid integer for int() or a valid decimal value for float(). But what happens when the user gives unexpected input? Try running the sample below and enter a non-integer value like "oops" or "2003.4" for the first question.

Try It Now


When int() or float() are given invalid string data to convert, they will throw a ValueError exception when those statements are executed.

In what year were you born? oops
Traceback (most recent call last):
File "code1.py", line 1, in <module>
intAnswer = int(input("In what year were you born? "))
ValueError: invalid literal for int() with base 10: 'oops'
As you know, exceptions will normally halt your program immediately, and that's something we'd like to avoid!

Catching Exceptions with "try / except"
Fortunately, it is possible to build a safety net in your code! When exceptions happen at run-time, you can "catch" them with protective code that will handle the error gracefully instead of halting the program with an ugly error message.

Whenever you write code that can possibly throw an exception, you can indent that code inside a "try" block. Start with the "try" keyword, followed by a colon. Then, underneath, indent the risky code inside the body of the "try" area.

try:
intAnswer = int(input("In what year were you born? "))
floatAnswer = float(input("How much do you want to spend on a milkshake? "))
Copy
To end the "try" block, use the "except" keyword, again followed by a colon. "except" defines another block of code that will run only if an exception happens from a statement inside the "try" block.

try:
intAnswer = int(input("In what year were you born? "))
floatAnswer = float(input("How much do you want to spend on a milkshake? "))
except:
print("You entered an incorrect value!")
Copy
As summarized in the diagram below, the statements inside a "try" block will run normally. If no exception happens (green path), the "except" block will be skipped and the program flow will continue after the indented "except" block. But if an exception happens anywhere inside a "try" block, the program flow (red path) will immediately move to the "except" block, and the remaining statements inside "try" are skipped.

Illustration of exception flow

We've improved our sample code with try / except around the risky parts. Run it, enter some invalid data and observe the results!

Try It Now


The statement(s) inside the "except" block will only run if some exception happens. Otherwise, the code will continue normally and the "except" statements are skipped. You can verify this by re-running the example and entering correct data that won't cause an exception.

Using "else" or "finally" with "try / except"
You may want to run some code after the "try / except" blocks are finished, and you can add optional "else" or "finally" blocks at the end. These statements will mark blocks of code that run either when no exceptions happened ("else") or no matter what happened ("finally").

We've added an "else" and "finally" block at the end of our example. Try running it with both valid and invalid inputs. Can you predict the messages that will be printed in each case?

Try It Now


Validating Numeric Ranges
When your program asks the user for a numeric value, will any value work, or should the answer be limited to a particular range? Imagine asking the user for his or her age, and getting a valid integer input like 9001. That's an unreasonably large number that may actually cause problems later in your program. The user also might enter a negative value like -50 that doesn't make any sense at all.

You may wish to validate a numeric input to make sure it is between some minimum and maximum value. A human age, for example, might be limited to an integer between 0 and 150. You don't need any special Python keywords or functions to handle this validation. Just write some "if" logic with comparison operators. In the example below, we limit the year to a number between 1900 and 2050 and the cost of a milkshake to a value between $0.50 and $10.00.

Try It Now


The exact nature of the error-handling logic will depend on your program. How should your "if" logic and "try/except" blocks be laid out to correctly validate your particular situation? What needs to be done when invalid input is detected? That's for you to decide!

Looping until Successful
Users will often want a chance to correct any mistakes made when entering data. It can be frustrating if your program simply prints an error message and quits when the first small problem arises! Consider writing loops to continue gathering user input until the correct data is received.

A "while" loop usually makes a good choice for validation. You want to loop "while" the user has not yet entered the correct information. Of course, the exact details are up to you and depend on your program needs. You might also consider a "for" loop that will only give the user a certain number of chances before giving up.

Let's return to our example one last time and add a "while" loop around each of the input values. Notice that we are now using two different "try / except" blocks - one for each of the values. One "while" loop surrounds the first input validation area and another "while" loop covers the second area. Run the code and watch how the program reacts to a variety of bad input values. The sample run below demonstrates all of the validation logic.

In what year were you born? twenty-ten
Please enter an integer
In what year were you born? 1850
Invalid year; must be between 1900 and 2050.
In what year were you born? 2005
Validated: 2005
How much do you want to spend on a milkshake? nothing
Please enter a decimal value
How much do you want to spend on a milkshake? 0
Invalid cost; must be between 0.50 and 10.00
How much do you want to spend on a milkshake? 5.50
Validated: 5.5
Again, your own programs may require different validation logic! You can make decisions like limiting the number of retries, giving hints, or performing different kinds of validations. As a programmer, you should be comfortable testing conditions, writing loops, and "if / else" logic to get the program behavior you want. The new "try / except" blocks are just another tool in your kit to help validate input values.

1.Why is user input validation important? What kinds of conversions are risky and may result in run-time exceptions?
2.How does the "try" statement form a block of protected code?
3.When do statements in the "except" block run?
4.What is the purpose of the optional "else" and "finally" statements? How are they different?
5.How can you validate numeric input is within a sensible range?
6.What kind of loop might be appropriate if you want to re-prompt the user for correct input until successful?
7.What kind of loop might be appropriate if you want to give the user a certain number of retries before quitting?

1. User input validation is important to ensure that the program receives the correct type of input and does not crash due to unexpected or invalid data. Risky conversions, such as converting a string to an integer or float, may result in run-time exceptions if the input does not match the expected data type.

2. The "try" statement forms a block of protected code by enclosing the risky code that may potentially throw an exception. If an exception occurs within the "try" block, the program flow will move to the corresponding "except" block.

3. Statements in the "except" block will run when an exception occurs within the "try" block. This block is used to handle the error gracefully instead of crashing the program.

4. The optional "else" statement in a "try/except" block runs if no exceptions occur within the "try" block. The "finally" block will run regardless of whether an exception occurred or not. The main difference is that "else" will only run if there are no exceptions, while "finally" will always run.

5. You can validate numeric input by using "if" logic with comparison operators to ensure the input falls within a specific range. For example, checking if a user's age is between 0 and 150.

6. A "while" loop might be appropriate if you want to re-prompt the user for correct input until successful. This allows the user to keep trying until valid input is provided.

7. A "for" loop with a specific number of retries might be appropriate if you want to give the user a certain number of chances to input correct data before quitting the program. This can provide a limit to the number of retries allowed.