# MeanvalueList.py Copyright (c) Kari Laitinen
# http://www.naturalprogramming.com
# 2006-05-27 File created.
# 2009-04-13 Last modification.
# This program stores the given numbers in a Python list
# before calculating their mean value. Python does not
# have the conventional fixed-size arrays that are common
# in other programming languages. Lists are dynamic arrays
# that can grow as is necessary.
# In this program list method append() is used to add new
# items to the end of the list. In program Reverse.py
# the same operation is carried out with the + operator.
print "\n This program calculates the mean value of" \
"\n the numbers you enter from the keyboard." \
"\n The program stops when you enter a letter.\n"
list_of_numbers = []
keyboard_input_is_numerical = True
while keyboard_input_is_numerical == True :
try :
print " Enter a number:",
number_from_keyboard = float( raw_input() )
list_of_numbers.append( number_from_keyboard )
except :
keyboard_input_is_numerical = False
sum_of_numbers = 0.0
for number_in_list in list_of_numbers :
sum_of_numbers = sum_of_numbers + number_in_list
mean_value = 0.0
if len( list_of_numbers ) > 0 :
mean_value = sum_of_numbers / len( list_of_numbers )
print "\n The mean value is: %f" % mean_value