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)

Recommended Answers

All 2 Replies

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.

Again.

  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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.