Take multiple inputs using a while loop in Python

avatar

Borislav Hadzhiev

Last updated: Aug 22, 2022

banner

Photo from Unsplash

Take multiple inputs using a while loop in Python #

To take multiple inputs using a while loop:

  1. Declare a new variable and initialize it to an empty list.
  2. Use a while loop to iterate until the list reaches a certain length.
  3. Use the list.append() method to append each input to the new list.
main.py
# ✅ Take multiple user input values until list contains min 3 items my_list = [] while len(my_list) < 3: user_input = input('Enter a color: ') my_list.append(user_input) print(my_list) # ---------------------------------------------- # ✅ Take multiple user input values until user types 'done' my_list = [] while True: user_input = input('Enter a color: ') if user_input == 'done': break my_list.append(user_input) print(my_list)

The examples use a while loop to take multiple inputs from a user.

The first while loop iterates until the list contains at least 3 items.

main.py
my_list = [] while len(my_list) < 3: user_input = input('Enter a color: ') my_list.append(user_input) print(my_list)

take multiple inputs while loop

Once the user enters 3 values, the condition is no longer met and we break out of the while loop.

On each iteration, we use the list.append() method to add the input value to the list.

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

Alternatively, you can use a while True loop.

main.py
my_list = [] while True: user_input = input('Enter a color: ') if user_input == 'done': break my_list.append(user_input) print(my_list)

The loop takes inputs from the user until they type done.

You can also use an empty string as a condition to exit out of the loop.

main.py
my_list = [] while True: user_input = input('Enter a color: ') if user_input == '': break my_list.append(user_input) print(my_list)

take multiple inputs while loop enter stop

Once the user presses Enter without typing in a value, the if block runs.

The break statement breaks out of the innermost enclosing for or 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.

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

If you need to take multiple integers from user input, make sure to use the int() class to convert each string to an integer.

main.py
my_list = [] while True: user_input = input('Enter a number: ') if user_input == '': break try: my_list.append(int(user_input)) except ValueError: print('Invalid number.') continue print(my_list)

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.

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.