# Program to show interactive while loop # Matt Bishop, May 4, 2009 # for ECS 10 Spring 2009 # computes an average def main(): sum = 0 count = 0 # go until user says enough ans = "yes" while ans == "yes": # read number # we include the addition in the try in case the user # enters a string, quoted or not try: n = input("Enter number: ") # add it, increment count sum += n; count += 1 except EOFError: # on end of file, we're done; quit gracefully ans = "no" except (NameError, TypeError): # non-number entered; complain print "You did not enter a number" # ask if there is any more, unless you found an end of file # ans is "yes" unless we found an end of file if ans == "yes": # again, we do error checking; as this is for a string, # we only need to worry about an end of file try: ans = raw_input("Do you want to input another number (yes or no)? ") except EOFError: # found end of file; quit gracefully ans = "no" # print average if count == 0: print "You didn't enter any numbers!" else: print "The average of the", count, "numbers you entered is", float(sum)/count main()