Question

Could anyone please help with this Python code assignment? In this programming assignment you are to...

Could anyone please help with this Python code assignment?

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!

The output from the program is to display the information described above using the following strings preceding the values. There is to be a space between the colon and the value.

File name:
Sum:
Count:
Average:
Maximum:
Minimum:
Range:

At the end of one attempt at reading, or a successful read, of a file the user it to be asked if they would like to evaluate another file of numbers. Use the prompt: Would you like to evaluate another file? (y/n) If the user answers y, then the program is to accept input for another file name. If the user answers with anything other than y, the program is to exit.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PYTHON CODE:

# import sys package
import sys

# loop begins
while True:


  
    try:

        # getting filename from the user
        filename=input('Enter the name of the file ')

        # opening the file
        f=open(filename)

        # variable for counting the integers
        c=0

        # variable to store the maximum number
        maxi=-(sys.maxsize)

        # variable to store the minimum number
        mini=sys.maxsize

        # variable to store the total
        total=0

        # variable to store the average
        avg=0.0

        # reading the file
        for line in f:

            # converting the line into integer
            i=int(line.strip())

            # incrementing the count
            c+=1

            # adding the number to the total variable
            total+=i

            # checking for maximum
            if i > maxi:
                maxi=i

            # checking for minimum
            if i < mini:
                mini=i

        # calculating average
        avg=total/c


        # printing the data
        print('File name: %s'% filename)
        print('Sum: %d' % total)
        print('Count: %d' % c)
        print('Average: %.2f' % avg)
        print('Maximum: %d' % maxi)
        print('Minimum: %d' % mini)
        print('Range: %d' % (maxi-mini))

        # closing the file
        f.close()

    except:

        # prints when file not found
        print('File not found')

  
    # getting user choice of continuing or quitting
    choice=input('Would you like to evaluate another file? (y/n) ')

    choice=choice.lower()

    # if the choice is y , then loop continues
    if choice== 'y':
        continue

    #otherwise quits
    else:
        break   

   
  

SCREENSHOT FOR OUTPUT:

Enter the name of the file data.txt File name: data.txt Sum: 851 Count: 10 Average: 85.10 Maximum: 400 Minimum: 3 Range: 397

CONTENTS OF data.txt:

8
20
30
400
50
3
70
80
90
100

Add a comment
Know the answer?
Add Answer to:
Could anyone please help with this Python code assignment? In this programming assignment you are to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Description: Create a program called numstat.py that reads a series of integer numbers from a file...

    Description: Create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the name of file, sum of numbers, count of numbers, average of numbers, maximum value, minimum value, and range of values. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information. Requirements: Create a program called numstat.py that reads a series of integer numbers from a file and determines...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.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...

  • In c++ programming, can you please edit this program to meet these requirements: The program that...

    In c++ programming, can you please edit this program to meet these requirements: The program that needs editing: #include <iostream> #include <fstream> #include <iomanip> using namespace std; int cal_avg(char* filenumbers, int n){ int a = 0; int number[100]; ifstream filein(filenumbers); if (!filein) { cout << "Please enter a a correct file to read in the numbers from"; }    while (!filein.eof()) { filein >> number[a]; a++; } int total = number[0]; for (a = 0; a < n; a++) {...

  • Your next programming assignment at the Research Center is to write a program that reads data...

    Your next programming assignment at the Research Center is to write a program that reads data from a file and produces a report of rainfall for that period of time. The program should read the starting month name from the data file, then read an ending month name, followed by reading in monthly rainfall amounts for each of the months from the starting month through the ending month. As it reads the monthly totals, it should sum (accumulate) the rainfall...

  • FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data...

    FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data as arguments (parameters) for a number of functions you will write. You will need to: Write and test a function square_each(nums) Where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, a return will not be allowed! Write and test a function sum_list(nums) Where nums is a...

  • In C please! Thank you! Write a program that finds the smallest (min), largest (max), and...

    In C please! Thank you! Write a program that finds the smallest (min), largest (max), and average (mean) of N values entered by the user. Your program should begin by prompting the user for N, the number of values to be entered. Then the program should accept that number of values, determining the min, max, and mean. Then it should display those values. This is an extension of a previous lab. First, get it working for N values. Then, extend...

  • Python Error: Writing a program to complete the following: Prompt the user for the name of...

    Python Error: Writing a program to complete the following: Prompt the user for the name of an input file. Open that file for input. (reading) Read the file using a "for line in a file" loop For each line,  o print the line and compute and print the average word length for that line. Close the input file Sample output: MY CODE IS WRONG AND NEED HELP CORRECTING IT!!! ------------------------------------------------------------------------------------------------------------------------ DESIRED OUTPUT: Enter input file name: Seasons.txt Thirty days hath September...

  • In this assignment, you will revisit reading data from a file, and use that data as...

    In this assignment, you will revisit reading data from a file, and use that data as arguments (parameters) for a number of functions you will write. You will need to: Write and test a function square_each(nums) Where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, return will not be allowed! Write and test a function sum_list(nums) Where nums is a (Python) list...

  • Lab 1 Q1. Write a Python program with the following function building block. •All input values...

    Lab 1 Q1. Write a Python program with the following function building block. •All input values are to be taken fro m the user. •Ensure to check for possible error cases like mismatch of data type and input out of range, for every function.       Function 1: countVowels(sentence) – Function that returns the count of vowels in a sentence. Function 2: Sum of all even numbers between two numbers, identify number of parameters   Q2. Write a Python program that reads a...

  • Assignment • No variables declared after you start writing code, must be declared at the top....

    Assignment • No variables declared after you start writing code, must be declared at the top. • Reuse all the variables from the first half in the second half, do not make up new variable names. • You code must be properly formatted. • You must use the code provided and variables names provided and not add any more variables to your code. • No for(int I = 0; … no declaring variables except at top of code. • You...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT