# # This program finds the average of all numbers in an input file # !!! We assume that all numbers are typed into a file one by one # def main(): print "This program finds the average of all numbers in a given file." print "Numbers should be entered into a file one per line." fname=raw_input("Please, input the file name, where the numbers are stored:") # If the file with the supplied name cannot be found, the program just prints the warning message and stops try: inp=open(fname,'r') sum, counter=0,0 for line in inp: sum=sum+eval(line) counter=counter+1 average=float(sum)/counter print "The average of all numbers from the file %s is %f" % (fname,average) except IOError: print "Can't find the file %s" % fname main()