Write a Hmmm program, average, ha, that repeatedly prompts for and reads in a se
ID: 3923001 • Letter: W
Question
Write a Hmmm program, average, ha, that repeatedly prompts for and reads in a sequence of integers and prints their average (rounded down, since we are dealing with prints the remainder (possibly 0). To mark the end of your sequence, enter 0. This last 0 should not be counted in the average (i.e. don't count it as an entry). Eg $ python hmmmAssembler. py Enter input file names average. ha. .. $ python hmmm inulator. py Enter debugging mode? no Enter number1 12 Enter number1 14 Enter number1 15 Enter number1 0 13 2 $ $ python hmmmimulator. Py Enter debugging mode? no 0 $Explanation / Answer
hmmAssembler.py: To take input from file
import os
from sys import argv
try:
while 1:
filename = raw_input("Please enter a filename: ")
txt = open(filename)
print "File name: "+filename
count = 0
sum1 = 0
for line in txt:
count = count + 1
sum1 = sum1 + int(line)
print "Average: "+str(sum1/count)
next_file = raw_input("Would you like to evaluate another file? (y/n) ")
if next_file != 'y':
break
except:
print "File not given or error while reading file!!!"
hmmSimulator.py: To take input from command line
import os
from sys import argv
count = 0
sum1 = 0
input1 = 0
while 1:
input1 = raw_input("Enter number: ")
input1 = int(input1)
if input1 != 0:
count = count+1
sum1 = sum1+int(input1)
else:
break
print "Average: "+str(sum1/count)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.