This program will take the input of numpy file Grades1.npy and produce an output summarizing the scores within the file and then save the output in separate files.
import numpy as np
def letterGrade(score): #returns corresponding letter grade for given numerical score
grade = 'unknown'
if score >= 93 and score <= 100:
grade = 'A'
elif score >= 90 and score <= 92.99:
grade = 'A-'
elif score >= 86 and score <= 89.99:
grade = 'B+'
elif score >= 83 and score <= 85.99:
grade = 'B'
elif score >= 80 and score <= 82.99:
grade = 'B-'
elif score >= 76 and score <= 79.99:
grade = 'C+'
elif score >= 73 and score <= 75.99:
grade = 'C'
elif score >= 70 and score <= 72.99:
grade = 'C-'
elif score >= 66 and score <= 69.99:
grade = 'D+'
elif score >= 60 and score <= 65.99:
grade = 'D'
elif score >= 0 and score <= 59.99:
grade = 'F'
elif score < 0 or score > 100:
grade = 'can not be determined from the input given'
return grade
#-----------------------------------------------------------------------------
def checkNeg(grades): #checks for negative values and replaces them with zero
grades1 = grades #returns array with corrected values and student id of record with replaced values
for i in range(len(grades1)):
for j in range(1,30):
if grades1[i][j] < 0:
grades1[i][j] = 0
id0 = grades[i][0]
return grades1, id0
#------------------------------------------------------------------------------
def indvScores(grades): #returns array with averages and final scores
arr1 = np.empty((len(grades),8))
arr2 = np.empty((len(grades),1), dtype = object)
for i in range(1, len(grades)):
sid = grades[i][0] #retrieve student id numbers
arr1[i][0] = sid
#retrieve and average homework scores
hw = np.sum(grades[i][1:7])
hw1 = hw/6
hpc = ((hw1 * .01) * 20)
arr1[i][1] = round(hpc,2)
#retrieve and average lab scores
labs = np.sum(grades[i][7:18])
labs1 = labs/11
lpc = ((labs1 * .01) * 30)
arr1[i][2] = round(lpc,2)
#retrieve final project score
final = grades[i][18]
fpc = ((final * .01) * 10)
arr1[i][3] = fpc
#retrieve midterm 1 score
midT1 = grades[i][19]
mpc = ((midT1 * .01) * 10)
arr1[i][4] = mpc
#retrieve and average quiz scores
qz = np.sum(grades[i][20:29])
qz1 = qz/9
qpc = ((qz1 * .1) * 15)
arr1[i][5] = round(qpc,2)
#retrieve midterm 2 score
midT2 = grades[i][29]
mpc2 = ((midT2 * .01) * 15)
arr1[i][6] = mpc2
#compile into a total score
total = (hpc + lpc + fpc + mpc + qpc + mpc2)
arr1[i][7] = round(total,2)
ltrGrd = letterGrade(total) #find letter grade
print("Final score for student", sid, "is",round(total,2),"for letter grade", ltrGrd )
print()
arr2[i][0] = ltrGrd
return arr1, arr2
#------------------------------------------------------------------------------
def multiRowAvg(grades, ncol, startcol, title, names): #returns array with averages and scores for grade groups
arr2 = np.empty((len(grades) ,ncol)) #create an empty array to populate with homework scores
for i in range(0, len(grades)):
for j in range(ncol):
arr2[i][j] = round(grades[i][j + startcol],2)
arr2 = np.delete(arr2, (0), axis = 0) #delete row with weights
hwav = arr2.mean(axis = 0) #averages for each homework assignment
for m in range(len(hwav)):
print(title,names[m],"is",round(hwav[m],2))
if ncol > 1:
hwav2 = hwav.mean(axis = 0)
print()
print("The average", title,"score was", round(hwav2,2))
arr3 = np.empty(ncol)
for m in range(len(hwav)):
arr3[m] = round(hwav[m],2)
arr2 = np.vstack([arr2,arr3])
elif ncol == 1:
arr3 = np.empty(ncol)
arr3 = hwav
arr2 =np.vstack([arr2,arr3])
return arr2
#-----------------------------------------------------------------------------
def main():
print("This program will take the input of numpy file Grades1.npy and produce")
print("an output summarizing the scores within the file and then save the output")
print("in separate files. Incorrect file paths will produce errors.")
print()
grades1 = np.load(input("Please input the path to the file "))
print()
grades, id0 = checkNeg(grades1) #function that checks for negative values
arr1, arr21 = indvScores(grades) #function that returns array with averages and final scores
arr1 = np.delete(arr1,(0),axis=0)
arr21 = np.delete(arr21,(0),axis=0)
arr21 = np.hstack([arr1, arr21])
np.save("letterGrades", arr21)
np.save("score_avgs", arr1) #save averages and final scores in file
names1 = [0,1,2,3,4,5] #print averages and save in separate files for each grade group
title1 = 'homework'
print("Average score for each homework")
print("--------------------------------")
arr2 = multiRowAvg(grades, 6, 1, title1, names1)
print()
print("Homework scores are saved in a file named homework_scores with the averages appended to row number", len(arr2))
print()
np.save("homework_scores", arr2)
names2 = [1,2,3,4,5,6,7,8,9,10,11]
title2 = 'lab'
print("Average score for each lab")
print("---------------------------")
arr4 = multiRowAvg(grades, 11, 7, title2, names2)
print()
print("Lab scores are saved in a file named lab_scores with the averages appended to row number", len(arr4))
print()
np.save("lab_scores", arr4)
names3 = [1,2,3,5,6,7,8,9,10]
title3 = 'quiz'
print("Average score for each quiz")
print("---------------------------")
arr6 = multiRowAvg(grades, 9, 20, title3, names3)
print()
print("Quiz scores are saved in a file named quiz_scores with the averages appended to row number", len(arr6))
print()
np.save("quiz_scores", arr6)
names4 = ['']
title4 = 'Final Project'
print("Average score for Final Project")
print("-------------------------------")
arr8 = multiRowAvg(grades, 1, 18, title4, names4)
print()
print("Final project scores are saved in a file named final_project_scores with the average appended to row number", len(arr8))
print()
np.save("final_project_scores", arr8)
names5 = [1]
title5 = 'Midterm'
print("Average score for Midterm 1")
print("---------------------------")
arr10 = multiRowAvg(grades, 1, 19, title5, names5)
print()
print("Midterm 1 scores are saved in a file named midterm1_scores with the average appended to row number", len(arr10))
print()
np.save("midterm1_scores", arr10)
names6 = [2]
title6 = 'Midterm'
print("Average score for Midterm 2")
print("---------------------------")
arr12 = multiRowAvg(grades, 1, 29, title6, names6)
print()
print("Midterm 2 scores are saved in a file named midterm2_scores with the average appended to row number", len(arr12))
print()
np.save("midterm2_scores", arr12)
totals = 0
for j in range(1,len(arr1)): #get average letter grade and print averages and final scores file
totals += arr1[j][7]
totalAvg = totals/(len(arr1))
ltrGavg = letterGrade(totalAvg)
print()
print("The average total score for the whole class was a",round(totalAvg, 2), "with the average letter grade being a", ltrGavg)
print("Student with id #", id0, "had a negative score entered in the original file")
print("Since negative scores are not possible, any records with a negative score will be changed to zero")
print()
print()
print("The results of this program will be saved in a file named letterGrades")
main()