Question #333677

In this exercise you will create a program that computes the average of a collection of values entered by the user. The user will enter 0 as a sentinel value to indicate that no further values will be provided. Your program should display an appropriate error message if the first value entered by the user is 0.


Expert's answer

print("Input some integers to calculate their sum and average. Input 0 to exit.")
count = 0
sum = 0
number = 1

while number != 0:
  number = int(input())
  sum = sum + number
  count += 1

if count == 1:
  print("Input some numbers")
else:
  average = sum / (count - 1)
  average = float('{:.3f}'.format(average))
  print("Average of the above numbers are: ", average)
LATEST TUTORIALS
APPROVED BY CLIENTS