Search⌘ K
AI Features

Solution: Compute the Average of an Arbitrary Number of Inputs

Understand how to handle console input in Python by capturing an arbitrary number of float inputs, processing them with loops, and computing their average. This lesson teaches you practical skills for managing user input and output in Python programs.

We'll cover the following...

The solution to the problem of receiving an arbitrary number of float inputs and computing their average is given below.

Solution

Python 3.8
# Enter the total number of inputs the user wants to enter e.g., 3
# then press enter/return
# Enter all number in a single line, use space as a delimiter e.g., 2.0 5.0 8.0
# then click the "Run" button
num = int(input('How many numbers do you wish to input: '))
print(num)
totalsum = 0
number = [float(x) for x in input('Enter all numbers: ').split( )]
print(number)
for n in range(len(number)) :
totalsum = totalsum + number[n]
avg = totalsum / num
print('Average of', num, 'numbers is:', avg)
Did you find this helpful?

Explanation

...