In this programming assignment you are to create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the following:

• The name of the file.

• The sum of the numbers.

• The count of how many numbers are in the file.

• The average of the numbers.

The average is the sum of the numbers divided by how many there are.

• The maximum value.

• The minimum value.

• The range of the values. The range is the maximum value minus the minimum value.

The program is to prompt the user for the name of the file that contains the numbers. If an exception occurs trying to open or read the file an error message is to be displayed. The program is not to crash if the file is not found or there is an error reading the file. Use try-except!

Answer :

Answer:

def main():

   filename = input('Enter the name of the file: ')

   num=0

   try:

     

       infile = open(filename, 'r')

       count = 0

       total = 0.0

       average = 0.0

       maximum = 0

       minimum = 0

       range1 = 0

       for line in infile:

           num = int(line)

           count = count + 1

           total = total + num

             

           if count == 1:

               maximum = num

               minimum = num

           else:

               if num > maximum:

                   maximum = num

               if num < minimum:

                   minimum = num

             

       if count > 0:

           average = total / count

           range1 = maximum - minimum      

       print('The name of the file: ', filename)

       print('The sum of the numbers: ', total)

       print('The count of how many numbers are in the file: ', count)

       print('The average of the numbers: ', average)

       print('The maximum value: ', maximum)

       print('The minimum value: ', minimum)

       print('The range of the values (maximum - minimum): ', range1)

     

       infile.close()

     

   except:

       print('The input file is not found!')

main()

Explanation:

Other Questions