# # 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 = 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: inSource = open(fname,'r') s, counter = 0,0 for line in inSource: s = s + float(line) counter = counter+1 average=s/counter print("The average of all numbers from the file {0:s} is {1:0f}".format(fname,average)) except FileNotFoundError: print("File is not found.") except ValueError: print("File contains non-numerical values") main()