Write a Hmmm program(Harvey Mudd Miniature Machine), average.ha, that repeatedly
ID: 3926569 • Letter: W
Question
Write a Hmmm program(Harvey Mudd Miniature Machine), average.ha, that repeatedly prompts for and reads in a sequence of integers and prints their average (rounded down, since we are dealing with integers) and prints the remainder (possibly 0). To mark the end of your sequence, enter 0. This last 0 should not be counted in the average (ie don't count it as an entry). Eg
Note: if just a 0 is entered, nothing should be printed.
Please use comments to provide a short narrative at the start of each program.
MUST BE IN HMMM***
Explanation / Answer
inp = 'Enter a number: ' total = 0 count = 0 average = 0 while True: s = raw_input(inp) if s == 'done': break try: total += float(s) count += 1 average = total / count except ValueError: print "Invalid Input. Try again: " continue print 'You entered %s numbers whose total is %s and average is %s.' % (str(count), str(total), str(average))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.