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:

    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]))+

    studentAverage2 = ((eval(listSplit2[2])*eval(listSplit2[3]))+

    studentAverage3 = ((eval(listSplit3[2])*eval(listSplit3[3]))+

    classAverage = (studentAverage1+studentAverage2+studentAverage3)/3

    print(listSplit1[0]+" "+listSplit1[1] , "'s average is: " ,
    print(listSplit2[0]+" "+listSplit2[1] , "'s average is: " ,
    print(listSplit3[0]+" "+listSplit3[1] , "'s average is: " ,
    print("Class average is: ", classAverage)

Edited by PyGuy123456789

1 Year
Discussion Span
Last Post by Gribouillis

Your approach will only work with 3 lines. You need to step back and work on it with a new design.

Think of writing it as if it was one line in the text file but you don't know how many tests there will be.

Sounds hard but the clue was given you in this sentence "Note the weights for each student will always add up to 100."

Aha, now read in the line and parse it, adding the points and weights till your totalweight is 100. Now print out that student result, increment your student count and add this score to your total score for use when you can't read more lines.


  1. Read a line.
  2. Parse, add until this student's weights get to 100.
  3. Print the student's results.
  4. Add 1 to the student count, add the score to the totalclass_score for use later.
  5. go get another line and do again until no more lines.
  6. Done with students, calculate the average for the class and print out that detail.

You can handle each student one after the other in the for line in infile loop and you don't need strList: use lineSplit = line.strip().split(). Also use int() instead of eval(). Eval is rarely used by python programmers.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.