pythonpython-3.xinputmultiple-input

Why and how multiple lines input works in Python


Before you report me for duplicate let me link similar topics which say how to write the code, but don't say how it works:

Now the code to read multiple lines:

'''
input data:
line 1
line 2
line 3
'''

line_holder = []

while True:
    line = input("\nPlease paste here lines :\n")
    if line:
        line_holder.append(line)
    else:
        break
for line in line_holder:
    print(line)

How I understand it:

So if there is a queue of inputs, how else can I reach it? How is it stored on the computer and why do I need to build list, to see it?


Solution

  • So if there is a queue of inputs, how else can I reach it?
    As written your loop does not access a queue - input takes input from stdin ... typically data entered from the keyboard by the user.

    How is it stored on the computer ...?
    Assume you are referring to the non-existent queue (see above) but when you append line to the list, you are storing that line in the list.

    ... and why do I need to build list, to see it?
    You don't - you could just print the line to see it, but if you want to use that data later you have to put it in some kind of container and a list is convenient.