# Compute Average Heights

By Monty, 3rd May 2015

Jengod at the English language Wikipedia [CC BY 2.5 (http://creativecommons.org/licenses/by/2.5), GFDL (www.gnu.org/copyleft/fdl.html) or CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/)], via Wikimedia Commons

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!

#!/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)

### Try it out:

Click on the **Forward** button. Use the scrollbars if necessary to see output.

### Like this:

Like Loading...

*Related*

## What do you think?