Question

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 and displays the following:

  • Name of the file.
  • Sum of the numbers.
  • Count of how many numbers are in the file.
  • Average of the numbers. The average is the sum of the numbers divided by how many there are.
  • Maximum value.
  • Minimum value.
  • 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.

Testing

Resource: numbers.txt.zip (Links to an external site.)Links to an external site.

Once you have written your program, you need to test it. A sample file called numbers.txt containing a list of integers is provided for testing. The file numbers.txt is contained in a file provided above called numbers.txt.zip. You must unzip numbers.txt.zip to get the numbers.txt file. You can also hand create files or use the random number writer from the earlier challenge to create files for testing.

The numbers.txt file, or any other test file containing numbers, needs to be in the same directory as the python program for it to be found.

For the provided numbers.txt file the following are the values your program should generate:

Sum: 56110
Count: 100
Average: 561.1
Maximum: 995
Minimum: 8
Range: 987

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

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()

Add a comment
Know the answer?
Add Answer to:
Description: Create a program called numstat.py that reads a series of integer numbers from a file...
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
  • 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...

  • 1. Write a program called Numbers that a. prompts the user for a file name. b....

    1. Write a program called Numbers that a. prompts the user for a file name. b. reads that file assuming that its contents consist entirely of integers. c. prints the maximum, minimum, sum, count (number of integers in the file), and average of the numbers. For example, if the file numberinput.dat has the following content: 4 -2 18 15 31 27 Your program should produce the following output: csc% java Numbers Enter file name: numberinput.daft Maximum31 Minimum- -2 Sum -...

  • Create a ruby program called create_chart.rb that reads in the values from the file average_grades.csv attached...

    Create a ruby program called create_chart.rb that reads in the values from the file average_grades.csv attached It should produce a bar graph named average.png, that is titled "Average Scores", with a maximum value of 100 and a minimum value of 50. average_grades.csv file. Usemame Average Tom 80 Adam 82 Tony Mike 85 94 Dave 100 Rob 75 Nick 80 93 Justin Jen 90

  • Write a program that reads the integer numbers in a text file (numbers.txt) and stores the...

    Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0 7. Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7...

  • Write a program that reads the integer numbers in a text file (numbers.txt) and stores the...

    Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0 IN JAVA PLZ 7. Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4...

  • 1.Write a python program that writes a series of random numbers to a file named random.txt....

    1.Write a python program that writes a series of random numbers to a file named random.txt. Each random number should be in the range of 1 through 300. The application should let the user specify how many random numbers the file will hold. 2. Write another program that reads the random numbers from the random.txt file, displays the numbers, then displays the following data: I. The total of the numbers II. The number of random numbers read from the file

  • import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads...

    import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads numbers from a file, calculates the mean and standard deviation, and writes the results to a file. */ public class StatsDemo { // TASK #1 Add the throws clause public static void main(String[] args) { double sum = 0; // The sum of the numbers int count = 0; // The number of numbers added double mean = 0; // The average of the...

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • Lab 10 Write a program that reads the integer numbers in a text file (numbers.txt) and...

    Lab 10 Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0

  • C++ Write a program based on 8_lb that reads in the integer data from "input.txt" into...

    C++ Write a program based on 8_lb that reads in the integer data from "input.txt" into a vector. Please prompt for the file name and append the "txt". Then create another vector where each element is the original vector times 10. Create a variable total and sum up the numbers from the second vector. The total should be 17510. Then write the data from the original vector and the 10 times vector to a file. See output.txt for the format....

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