This is a simple program to compute average heights. In a loop, it prompts the user for heights, inputs a text string and converts it to a ‘real’ number, sums them, and divides by the number of heights. Exit is signalled by negative height, after which the sum of the numbers is divided by the number of numbers. There are (at least) 2 errors. Find and fix!
[python] #!/usr/bin/python3# This is a simple program to compute average heights.
# It prompts user for heights, sums them, and divides by the number of heights.
# Exit is signalled by negative height. There are (at least) 2 errors. Find and fix!
# Authour: Alan Richmond, Python3.codes
sum = 0.0
num = 0
done = False
while not done:
height = float(input("Enter a height or 0: "))
if height < 0:
done = True
else:
sum += height
num += 1
print ("Average height is ", sum / num)
[/python]
Try it out:
Click on the Forward button. Use the scrollbars if necessary to see output.
In colloquial language, an average is the sum of a list of numbers divided by the number of numbers in the list. In mathematics and statistics, this would be called the arithmetic mean. However, the word average may also refer to the median, mode, or other central or typical value. In statistics, these are all known as measures of central tendency.