Question

Topics: list, file input/output (Python) You will write a program that allows the user to read...

Topics: list, file input/output (Python)

You will write a program that allows the user to read grade data from a text file, view computed statistical values based on the data, and to save the computed statistics to a text file. You will use a list to store the data read in, and for computing the statistics. You must use functions.

The data:

The user has the option to load a data file. The data consists of integer values representing student grades in a class. The value ranges from 0 to 100. The data is stored in a text file with each value separated by a white space. You may assume that that number of data points in the file will never exceed 1,000.

Descriptive Statistics:

The user has the option to compute statistics on the grade data. Your program will compute the following values:

  • Min - the lowest value in the data
  • Max     -the highest value in the data
  • Mean   - the average value
  • Median – the middle value of the sorted values

To compute the median, you must first sort the data. When the data size is odd, there’s only one middle value to pick from as the median. When the data size is even, there are two middle values and you can pick either one as the median.

Saving result:

The user has the option to save the statistics to a file. Your program will save the descriptive statistics result to a text file, each value per line. The format as in the sample below:

Min      = 35

Max      = 100

Mean = 79

Median     = 73

Sample run:

Choose an option:

1. Load data

2. Display computed statistics

3. Save computed statistics

4. Exit

Choice: 1

Enter the name of the file: data.txt

Data read successfully.

Choose an option:

1. Load data

2. Display computed statistics

3. Save computed statistics

4. Exit

Choice: 2

Below are the compute values:

Min       = 35

Max       = 100

Mean = 79.3

Median      = 73

Choose an option:

1. Load data

2. Display computed statistics

3. Save computed statistics

4. Exit

Choice: 3

Enter the name of the file: data-stats.txt

Result saved successful.

Choose an option:

1. Load data

2. Display computed statistics

3. Save computed statistics

4. Exit

Choice: 4

Goodbye!

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

If you have any doubts, please give me comment...

data = []

while True:

    print("Choose an option:")

    print("1. Load data")

    print("2. Display computed statistics")

    print("3. Save computed statistics")

    print("4. Exit")

    choice = int(input("Choice: "))

    if choice == 1:

        fname = input("Enter the name of the file: ")

        file = open(fname)

        contents = file.read().split()

        file.close()

        for el in contents:

            data.append(int(el))

        data.sort()

        mean = sum(data)/len(data)

        mid = len(data)//2

        if len(data)%2==0:

            median = data[mid]

        else:

            median = (data[mid]+data[mid+1])/2

        print("Data read successfully.")

    elif choice==2:

        if len(data)==0:

            print("data is empty")

        else:

            print("Min\t=",min(data))

            print("Max\t=",max(data))

            print("Mean\t= %.2f"%mean)

            print("Median\t= %.2f"%median)

    elif choice==3:

        if len(data)==0:

            print("data is empty")

        else:

            fname = input("Enter the name of the file: ")

            fout = open(fname, "w")

            print("Min\t= %d"%min(data), file=fout)

            print("Max\t= %d"%max(data), file=fout)

            print("Mean\t= %.2f"%mean, file=fout)

            print("Median\t= %.2f"%median, file=fout)

            fout.close()

            print("Result saved successful.")

    elif choice==4:

        print("Goodbye!")

        break

    else:

        print("Invalid choice!")

Add a comment
Know the answer?
Add Answer to:
Topics: list, file input/output (Python) You will write a program that allows the user to read...
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
  • The file lab10movies.cpp contains a program that allows the user to read movie names and movie re...

    Please answer using C++ The file lab10movies.cpp contains a program that allows the user to read movie names and movie release dates from a file called movies.txt. The data is read into a single ended linked list. Once all the data has been read in, the user is given a menu option with 3 choices. Choice 1 will list all movies released before a specified year, Choice 2 will list all movies stored in the linked list, and Choice 3...

  • Write a program to read the contents of data.txt file, and determine the smallest value (min),...

    Write a program to read the contents of data.txt file, and determine the smallest value (min), largest value (max), and the average value (ave). The minimum, maximum, and average values must be calculated in the function calc(). Remember to declare the stdlib.h library, so that you can use the atof function, which will convert the number in the text file into float number. Also, remember to check if there is an error opening the file or not. If there is...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Write a contacts database program that presents the user with a menu that allows the user...

    Write a contacts database program that presents the user with a menu that allows the user to select between the following options: (In Java) Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Use C++ For this week’s lab you will write a program to read a data file...

    Use C++ For this week’s lab you will write a program to read a data file containing numerical values, one per line. The program should compute average of the numbers and also find the smallest and the largest value in the file. You may assume that the data file will have EXACTLY 100 integer values in it. Process all the values out of the data file. Show the average, smallest, largest, and the name of the data file in the...

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • Function / File Read (USING MATLAB) A) create a user-defined function : [maxsample , maxvalue , numsamples]=writetofile(sname,strgth) to do the following: The user-defined function shall: -Accept as t...

    Function / File Read (USING MATLAB) A) create a user-defined function : [maxsample , maxvalue , numsamples]=writetofile(sname,strgth) to do the following: The user-defined function shall: -Accept as the input a provided string for the sample name (sname) and number for strength (strgth) - open a text file named mytensiledata.txt, with permission to read or append the file. - Make sure to capture any error in opening the file - Write sname and strgth to the file - Close the file...

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