How to take multiple inputs in Python using while loop | Example code

  • by

Use While True to take multiple inputs in Python using a while loop. Don’t forget to break the loop based on input.

Example take multiple inputs in Python using while loop

Simple example codes Multiple Inputs with Python using While Loop. Exit the loop if the user enters “stop”.

while True:
    reply = input("Enter Text: ")
    if reply == 'stop':
        break
    print(reply)

Output:

Another example

Take multiple inputs and append them to the list. If you use an infinite loop then break it if the input is 0.

values = []
while True:
    val = int(input("Enter an integer, 0 for Exit: "))
    if val == 0:
        break
    values.append(val)

print(values)

Output:

Enter an integer, 0 for Exit: 1
Enter an integer, 0 for Exit: 2
Enter an integer, 0 for Exit: 0
[1, 2]

Do comment if you have any doubts or suggestions on this Python multiple input code.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.