Python: Asking for user input until valid response is given

avatar

Borislav Hadzhiev

Last updated: Aug 23, 2022

banner

Photo from Unsplash

Python: Asking for user input until valid response is given #

To keep asking for user input until a valid response is given:

  1. Use a while loop to iterate until the input is valid.
  2. On each iteration, check if the input is one of the expected values.
  3. If the condition is met, use the break statement to break out of the loop.
main.py
# ✅ keep asking for user input until valid integer is given 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 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') # ---------------------------------------------------- # ✅ keep asking for user input until a 1 character response is given user_input = '' # 👇️ iterates until the user types in a single character while len(user_input) != 1: user_input = input('Enter a single character: ') print(user_input)

The first example keeps asking for user input until a valid integer is entered.

keep asking for user input until valid response

We used a while loop to keep asking the user for input until a valid integer is given.

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.

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 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')
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)

keep asking for user input until valid 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: ') # 👇️ input must be at least 5 characters and not in the list if len(password) < 5 or password in common_passwords: print('Pick a strong password') continue else: print(f'You entered {password}') break print(password)

keep asking for user input multiple conditions or

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 asking for user 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.