Receiving data on multiple lines

Asked

Viewed 34 times

-3

I need to create a code that will calculate the average, variance and standard deviation of the data entered by the user.

I thought of throwing this data into a list and from then on working them out. However, the user will type each number into a row. Capturing this data, each in a row, on a list is being my difficulty. I tried to work with the sys module and put a loop inside sys.stdin:

for n in sys.stdin:
     for x in range(n):
          lista.append(n)

But I’ve had some mistakes.

Could someone help me? I don’t necessarily want to work with the sys module, it was just an idea.

1 answer

-1


Isabela, the error probably exists because this method usually leaves an empty line after it is closed, which can ruin mathematical operations. Try to add x.rstrip() to the code and see if it solves.

Another method is using fileinput:

import fileinput

for x in fileinput.input():
    lista.append(x)

Fileinput also has the same blank line issue, so the x.rstrip() would also be necessary.

In case you need to use a better data structure further, I suggest taking a look at lib Pandas.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.