Repeat program until user Input is correct in Python

avatar

Borislav Hadzhiev

Last updated: Aug 23, 2022

banner

Photo from Unsplash

Repeat program until user Input is correct in Python #

To repeat a program until the user input is correct:

  1. Use a while loop to iterate until the input is correct.
  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
# ✅ When taking strings as input my_list = [] user_input = '' while True: user_input = input('Enter a string: ') # 👇️ Take input until Enter is pressed without value if user_input == '': print('User pressed enter') break my_list.append(user_input) print(my_list) # --------------------------------------------- # ✅ When taking integers as input my_list = [] user_input = '' while True: user_input = input('Enter a number: ') # 👇️ Take input until Enter is pressed without value if user_input == '': print('User pressed enter') break try: my_list.append(int(user_input)) except ValueError: print('Invalid number.') continue print(my_list)

repeat program until user input is correct

The examples repeat the program and keep taking user input until the user presses Enter without typing in a value.

This could be any other condition, e.g. if the user types done or if the list stores at least 3 input values.

The first example takes strings as input from the user.

main.py
my_list = [] user_input = '' while True: user_input = input('Enter a string: ') # 👇️ Take input until Enter is pressed without value if user_input == '': print('User pressed enter') break my_list.append(user_input) print(my_list)

We used a while loop to take user input while iterating an arbitrary number of times.

The only way to break out of a while True loop is to use a break statement or to raise an exception.

If the user presses Enter without typing in a value, we break out of the while loop.

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

Otherwise, we append the input value to the list.

The list.append() method adds an item to the end of the list.

Here is an example that keeps repeating the program until the user input is correct when taking integers.

main.py
my_list = [] user_input = '' while True: user_input = input('Enter a number: ') # 👇️ Take input until Enter is pressed without value if user_input == '': print('User pressed enter') break try: my_list.append(int(user_input)) except ValueError: print('Invalid number.') continue print(my_list)

repeat program until user input is correct integers

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.

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

We used the int() class to convert each string to an integer.

The try/except statement is used to handle the ValueError that is raised if an invalid integer is passed to the int() class.

Alternatively, you can use a while loop with a condition.

Here is an example that repeats the program until the user enters a single character.

main.py
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)

repeat program until user input is correct condition

Instead of using a while True loop with a break statement, we used a while loop with a condition.

If the user enters more than one character, the loop continues running.

Once the user enters a single character, the condition in the while loop is no longer met, so we break out 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.