How to take continuous input in Python | Example code

  • by

Use While loop with True condition expression to take continuous input in Python. And break the loop using if statement and break statement.

while True:

   some_method()

Example take continuous input in Python

Simple example code gets user input repeatedly until want to quit.

while True:

    user_input = input("Enter something: ")
    if user_input == "quit":
        break

print(user_input)

Output:

How to take continuous input in Python

And if you want to store input data use this code.

input_string = ''
while 1:
    inp = input('Add to a string: ')
    if inp == 'quit':
        break
    input_string += inp

print(input_string)

Output:

Add to a string: Hello
Add to a string: quit
Hello

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

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.