Given Problem:
A professor at Hardtack University has an unusual method of grading. The students may or may not all take the same number of tests. The individual tests are weighted, and these weights are used to compute the student's average. Important: the weights for all of the tests for a given student must add to 100. Assuming that w1 ... wn are the weights, and g1 ... gn are the grades, the average is computed by the formula:
((w1 * g1) + (w2 * g2) + ... + (wn * gn)) / 100
The names of the students and their grades, with weights, are stored in a text file in the format firstName lastName w1 g1 w2 g2 ... wn gn
Write the program weightedAverage.py. Ask the user to enter the name of a file of grades. Compute and print each student's average, using the formula given above. Then compute and print the class average, an average of the individual averages. Format all averages to one decimal place. Note the weights for each student will always add up to 100.
For example, if the text file contains:
Billy Bother 20 89 30 94 50 82
Hermione Heffalump 40 93 60 97
Kurt Kidd 20 88 30 82 40 76 10 99
Heres the code I have so far but it only works for the three sentences given, how do I make it work for any number of sentences
def weightedAverage():
infile = open(file, "r")
strList = []
for line in infile:
strList.append(line)
listSplit1 = strList[0].split()
listSplit2 = strList[1].split()
listSplit3 = strList[2].split()
g, w = values[::2], values[1::2]
studentAverage1 = ((eval(listSplit1[2])*eval(listSplit1[3]))+
(eval(listSplit1[4])*eval(listSplit1[5]))+
(eval(listSplit1[6])*eval(listSplit1[7])))/100
studentAverage2 = ((eval(listSplit2[2])*eval(listSplit2[3]))+
(eval(listSplit2[4])*eval(listSplit2[5])))/100
studentAverage3 = ((eval(listSplit3[2])*eval(listSplit3[3]))+
(eval(listSplit3[4])*eval(listSplit3[5]))+
(eval(listSplit3[6])*eval(listSplit3[7]))+
(eval(listSplit3[8])*eval(listSplit3[9])))/100
classAverage = (studentAverage1+studentAverage2+studentAverage3)/3
print(listSplit1[0]+" "+listSplit1[1] , "'s average is: " ,
studentAverage1)
print(listSplit2[0]+" "+listSplit2[1] , "'s average is: " ,
studentAverage2)
print(listSplit3[0]+" "+listSplit3[1] , "'s average is: " ,
studentAverage3)
print()
print("Class average is: ", classAverage)