# Program to average numbers
# (user enters number of numbers, then numbers)
# Matt Bishop, Apr. 24, 2009
# for ECS 10 Spring 2009
# add the numbers and return the sum
# parameter: listnum, a list of numbers
# returns: their sum
def addup(listnum):
# initialize sum
sum = 0;
# add each number in list to sum
for i in listnum:
sum = sum + i;
# return sum
return sum;
# compute average of a list of numbers
# parameters: numbers, a list of numbers
# count, number of numbers to average
# returns: their (floating point) average
def average(numbers, count):
# get the sum of the numbers
sum = addup(numbers);
# return the average
return float(sum) / count;
# read a list of numbers and print the average
# calls: function addup
def main():
# prompt for count of numbers to be entered
n = input("How many numbers do you want to add? ");
# initialize list variable
numbers = [];
# ask user for next number and
# append each number to the list
for i in range(n):
prompt = "Enter number %d: " % (i + 1);
numbers = numbers + [ input(prompt) ];
# now compute and print average
print "The average of your numbers is", average(numbers, n);
main();