4

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!

m0nhawk
  • 22,980
  • 9
  • 45
  • 73
Prunecandy
  • 53
  • 4
  • Small suggestion - when you don't really use variable - _i_ in you case, just for number of iteration - you may replace it with underscore *for _ in range(N)*. This usually helps IDEs from screaming at you :-) – volcano Apr 16 '17 at 20:14

3 Answers3

3

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.

George Bou
  • 568
  • 1
  • 8
  • 17
  • Wow you guys are fast! This is exactly what I needed. I had tried simply changing lines to [] before but did not keep lines.append in the for loop (dohhh). Thanks! – Prunecandy Apr 16 '17 at 20:17
  • Glad that we helped. You can always give the check mark to the answer that helped you so the poster gets the credit. :) – George Bou Apr 16 '17 at 20:20
0

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.

Community
  • 1
  • 1
LLL
  • 3,566
  • 2
  • 25
  • 44
0

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)
Vinícius Figueiredo
  • 6,300
  • 3
  • 25
  • 44