-1

I know the easiest way to take input in python is with the function input() .

However, what if I have to take several numbers on separate lines at once and add them up.

Example input:

2
3
1
4

How can I read this input into a list?

I am a beginner in python so please any advice is appreciated

momo12321
  • 141
  • 1
  • 9
  • Will the input be passed together? Or will it be added as separate inputs? You can use `input().split('\n')` otherwise `x1 = input()`, `x2 = input` etc.. – Alan Kavanagh Feb 27 '19 at 12:34
  • Maybe it can helps you : https://stackoverflow.com/questions/30239092/how-to-get-multiline-input-from-user – Rob Cime Feb 27 '19 at 12:36

1 Answers1

0

You can do something like this:

numbers_list = []
for i in range(number_of_needs):
    n = input()
    number_list.append(n)

or you can use while if you don't know the count.

numbers_list = []
while True:
    n = input()
    if n == 'q':
        break
    number_list.append(n)

in this case, it breaks when user inputs q.

also, note that ns are String you can use int() to convert them to an integer.

Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59