Input validation in a While loop in Python

avatar

Borislav Hadzhiev

Last updated: Aug 22, 2022

banner

Photo from Unsplash

Input validation in a While loop in Python #

To validate input in a while loop:

  1. Use a try/except or an if/else statement to validate the input.
  2. If the input is invalid, use a continue statement to continue to the next iteration.
  3. If the input is valid, use a break statement to break out of the loop.
main.py
# ✅ Input validation in while loop with exception handling 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 print(num) if num > 100: print('The provided number is greater than 100') elif num == 100: print('The provided number is equal to 100') else: print('The provided number is less than 100') # ------------------------------------------------------ # ✅ Input validation in while loop with if/else statement password = '' while True: password = input('Enter your password: ') if len(password) < 5: print('Password too short') continue else: print(f'You entered {password}') break print(password)

The first example validates an input in a while loop with exception handling.

The code snippet keeps prompting the user for input until they enter a valid integer.

main.py
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 print(num) if num > 100: print('The provided number is greater than 100') elif num == 100: print('The provided number is equal to 100') else: print('The provided number is less than 100')

input validation while loop exceptions

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 continue statement continues with the next iteration of the loop.

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

When validating user input in a while loop, we use the continue statement when the input is invalid, e.g. in an except block or an if statement.

If the input is valid, we use the break statement to exit out of the while loop.

You can use the same approach to validate a user input using an if/else statement.

main.py
password = '' while True: password = input('Enter your password: ') if len(password) < 5: print('Password too short') continue else: print(f'You entered {password}') break print(password)

input validation while loop if else

The while loop keeps iterating until the user enters a value that has a length of at least 5.

If the value is too short, we use the continue statement to continue to the next iteration.

If the value is at least 5 characters long, we use the break statement as the input is valid.

You can use the boolean or and and operators if you need to check for multiple conditions.

main.py
password = '' common_passwords = ['abcde', 'password123'] while True: password = input('Enter your password: ') if len(password) < 5 or password in common_passwords: print('Pick a strong password') continue else: print(f'You entered {password}') break print(password)

The if statement checks if the password is less than 5 characters or is in the commonly used passwords list.

We used the boolean or operator, so the if block runs if either of the two conditions is met.

If the password is less than 5 characters or is contained in the commonly used passwords list, we continue to the next iteration and prompt the user again.

Use the and boolean operator if you need to check if multiple conditions are met when validating the input.

main.py
password = '' common_passwords = ['abcde', 'password123'] while True: password = input('Enter your password: ') if len(password) > 5 and password not in common_passwords: print(f'You entered {password}') break else: print('Pick a strong password') continue print(password)

We used the and boolean operator, so for the if block to run both conditions have to be met.

The password has to be longer than 5 characters and it has to not be in the commonly used passwords list.

If the conditions are met, we use the break statement to exit out of the while True loop.

If the conditions aren't met, we use the continue statement to continue to the next iteration.

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.