Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON Calculate a weighted grade from data contained in a file. (A weighted tot

ID: 674308 • Letter: P

Question

PYTHON

Calculate a weighted grade from data contained in a file. (A weighted total is computed by multiplying each grade by the corresponding weight and adding them together.)

Your program should first prompt the user for the name of the file they want to read the data from. (You can assume the file will open successfully). After you open the file, you will use the data inside (weight and scores) to calculate and print out the user’s final weighted score.

The file will be formatted as follows:

One or more lines of numbers, separated by spaces.

On each line, the first number is a decimal (the weight for that type of assignment). You do not need to check that the weights add up to 1

All following numbers are integers. (The different scores for that type of assignment.) You do not need to check that the scores are valid.

May have different amounts of scores for each assignment type. May have any number of assignment types (but at least 1).

For example, the line

0.45 72 100 58 44 93 89 78 92

means that this type of assignment is worth 45% of the total grade. There are eight total assignments of this type. We can calculate the average of these eight assignments to be 78.25. If we multiply 78.25 by the weight, we can calculate this part of the weighted grade to be 35.2125 %. If the file has more than one line, add the weight of each line together x100 (to get the percent from the float) for the final grade.

Explanation / Answer

#!/usr/bin/env python3 # -*- coding: utf-8 -*- # file grades_example.py # author: # Example of I/O for the "grades" assignment 1 and of "stub" programming ''' This script reads an input .cvs file with: first name surname 12 homework grades 12 quiz scores (10 pts each) 4 exam scores (100 pts each) line at a time and reports first and surname averages for each category of score averall average and letter grade, per the schedule: homework - 40%; quizes - 10%; exams - 50% [100, 90] - A; (90, 80] - B; (80, 70] - C; (70, 60] - D; (60, 0] - F ''' from FileUtilities import openFileReadRobust as openRead, openFileWriteRobust as openWrite # Stub function for processing student data def processStudent(data, possible): line=sum([0 if k == '-' else int(k) for k in line[2:14]])/totals[hwtot] return ["blow, joe", 53, 26, 12, 34] # Stub function for setting up files for I/O def setUpIO(): return infile, outfile # return inFile, outFile # Opens the input and output files infile = open('grades.csv','r') outfile = open('grades.grd','w') # Get the homework, quiz, and exam possible totals line = infile.readline().rstrip(' ').split(',') hwtot = sum([0 if k == '-' else int(k) for k in line[2:14]]) qztot = 100 extot = 300 totals = [hwtot, qztot, extot] gradelist = [] # For every line in the file, compute the averages and final grade for line in infile: # process line and add it to the list of grades stuData = processStudent(line, totals) # Add student to grade list # Note: this next step could have been done much more elegantly, but # I wanted you to see the formatting gradelist.append('{:8}' gradelist.insert(0, gradestr.format('Name', 'HWavg', 'QZavg', 'EXavg', 'Final')) # Write the list to the output file outfile.write(' '.join(gradelist) + ' ') # Close files infile.close() outfile.close()