Reputation: 53
I know this is probably something incredibly simple, but I seem to be stumped. Anyways for an assignment I have to have the user enter the number of data points(N) followed by the data points themselves. They must then be printed in the same manner in which they were entered (one data point/line) and then put into a single list for later use. Here's what I have so far
N = int(input("Enter number of data points: "))
lines = ''
for i in range(N):
lines += input()+"\n"
print(lines)
output for n = 4 (user enters 1 (enter) 2 (enter)...4 and the following is printed:
1
2
3
4
So this works and looks perfect however I now need to convert these values into a list to do some statistics work later in the program. I have tried making a empty list and bringing lines into it however the /n formating seems to mess things up. Or I get list index out of range error. Any and all help is greatly appreciated!
Upvotes: 4
Views: 1304
Reputation: 6518
You could try to append all the data into a list first, and then print every item in it line by line, using a for loop to print every line, so there is no need to concatenate 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)
Upvotes: 0
Reputation: 3771
You could just add all the inputs to the list and use the join function like this:
'\n'.join(inputs)
when you want to print it. This gives you a string with each members of the list separated with the delimiter of your choice, newline in this case.
This way you don't need to add anything to the values you get from the user.
Upvotes: 0
Reputation: 588
How about adding every new input directly to a list and then just printing 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 every item was printed in a new line and you have a list to manipulate.
Upvotes: 3