How to average multiple Python inputs in a compressed form?

I have many, many numbers to write, and it's just not efficient to write out ...

a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))
c = float(input("Enter the third number: "))

... when you have a thousand numbers. So, how can I use a range function, perhaps to get a lot of numbers from one line of input and then calculate the average or average value of all inputs?

+4
source share
3 answers

One way is to use for a loop to re-query numbers. If only an average value is required, simply increase one variable and divide by the total number of queries at the end:

n = 10
temp = 0
for i in range(n):
    temp += float(input("Enter a number"))

print("The average is {}".format(temp / n))

sum() , :

n = 10
average = sum(float(input("Enter a number")) for i in range(n)) / n
print("The average is {}".format(average))
+4

.

nums = [float(input('enter number {}: '.format(i+1))) for i in range(100)]

100 . . n- nums[n-1].

:

>>> nums = [float(x) for x in input('enter numbers: ').split()]
enter numbers: 1.0 3.14 7.124 -5
>>> nums
[1.0, 3.14, 7.124, -5.0]

, .

, , :

import sys

nums = [float(x) for x in sys.argv[1:]]
print(nums)

:

$ python3 getinput.py 1.0 -5.37 8.2
[1.0, -5.37, 8.2]

, sum(nums)/len(nums), , .

+3

, "=" , , , .

-2

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


All Articles