Using try/except statement to validate user Input in Python

avatar

Borislav Hadzhiev

Last updated: Aug 23, 2022

banner

Photo from Unsplash

Using try/except statement to validate user Input in Python #

To use a try/except statement to validate user input:

  1. Wrap a call to the input() function in a try/except statement.
  2. Handle any errors in the except block.
main.py
# ✅ Validate user input with try/except num = 0 while True: try: num = int(input("Enter your favorite integer: ")) except ValueError: print("Please enter a valid integer") continue else: print(f'You entered: {num}') break # --------------------------------------------------------- # ✅ Validate user input with try/except and if/else statement # 👇️ provided integer must be in the range 1-10 num = 0 while True: try: num = int(input("Enter an integer 1-10: ")) except ValueError: print("Please enter a valid integer 1-10") continue if num >= 1 and num <= 10: print(f'You entered: {num}') break else: print('The integer must be in the range 1-10')

try except input validation

The first example uses a try/except statement when taking integers as input from the user.

If the code in the try block raises a ValueError, the except block runs, where we use the continue statement to continue to the next iteration.

If the user enters a valid integer, the try block completes successfully and then the else block runs where we use the break statement to exit out of the while loop.

The input function takes an optional prompt argument and writes it to standard output without a trailing newline.

The function then reads the line from input, converts it to a string and returns the result.

Note that the input() function is always guaranteed to return a string, even if the user enters a number.

You might also use a try/except statement with conditional statements to validate user input.

Here is an example that takes an integer in the range 1-10.

main.py
num = 0 while True: try: num = int(input("Enter an integer 1-10: ")) except ValueError: print("Please enter a valid integer 1-10") continue if num >= 1 and num <= 10: print(f'You entered: {num}') break else: print('The integer must be in the range 1-10')

try except input validation with conditionals

If the try block completes successfully, then the user entered an integer.

The if statement checks if the integer is in the range 1-10 and if the condition is met, we break out of the while loop.

The break statement breaks out of the innermost enclosing for or while loop.

If the integer is not in the specified range, the else block runs and prints a message.

If the user didn't enter an integer, the except block runs, where we use the continue statement to prompt the user again.

The continue statement continues with the next iteration of the loop.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.