Python: how to take a few lines of input and put them in a list

I know this is probably something incredibly simple, but I seem dumb. In any case, for the assignment, I need the user to enter the number of data points (N), followed by the data points themselves. Then they should be printed the same way they were entered (one data point / line), and then placed on one list for later use. That's what i still have

N = int(input("Enter number of data points: "))
lines = ''
for i in range(N):
   lines += input()+"\n"
print(lines)

for n = 4 (the user enters 1 (input) 2 (input) ... 4 and the following is printed:

1
2
3
4

So, this works and looks perfect, but now I need to convert these values ​​to a list so that some statistics work later in the program. I tried making an empty list and entering lines in it, but it seems that the / n structure will ruin everything. Or I get an index index out of range. Any help is appreciated!

+4
source share
3 answers

How to add each new entry directly to the list, and then just print it.

Like this:

N = int(input("Enter number of data points: "))
lines = []
for i in range(N):
    new_data = input("Next?")
    lines.append(new_data)

for i in lines:
    print(i)

Now each item has been printed on a new line, and you have a list to manage.

+3
source

You can simply add all the entries to the list and use the join function as follows:

'\n'.join(inputs)

. , , .

, .

0

First you can add all the data to the list first, and then print each item in it in turn, using the for loop to print each line, so there’s no need to combine it with "\n"

N = int(input("Enter number of data points: "))    
data = []
for i in range(N):
    item = data.append(input())

for i in data:    
    print(i)
0
source

Source: https://habr.com/ru/post/1674927/


All Articles