# # Sep 16 homework # chap 3 exercise 14 # Jim Mahoney's example solution. # # Find the average of numbers entered by the user. # # The problem doesn't say how to determine how # many numbers will be entered. There are several approaches: # (1) Ask the user how many numbers will be entered, # then use a "for" loop to ask again that many times. # (2) Ask the user to type a list, i.e. [2, 3, 6, 10], # then use a "for" loop to iterate over that list. # (3) Ask for one number at a time, with some special # signal (like a blank line) to indicate that # there are no more numbers. This approach is # a bit beyond us now, though it'll work once we # learn string input (to detect the blank line) # and conditionals (to do something different # when we get to the blank line). # I'll implement approach (1) here. print "-- chap3ex14.py --" print "Find the average of numbers entered by the user." def find_average(): count = input("How many numbers will be input? ") total = 0.0 # floating point, not integer. for i in range(1, count+1): number = input(" number " + str(i) + ": ") total = total + number print "The average is ", (total / count) find_average()