How can I calculate a weighted average?

Question

How can I calculate a weighted average?

Answer

This part is a bit tricky, but basically we’re adding up parts to make a whole. That is, after all, the point of weighting these averages.
So if we say homework is 10%, quizzes are 30%, and tests are 60%, that accounts for 100% of the pie. When we go to return our value, then, we just multiply each average that we’ve calculated by its respective weight in decimal form, like this:
return (part_1 * part_1_weight) + (part_2 * part_2_weight) + (part_3 * part_3_weight)

def get_average(student):
homework = average(student[“homework”])
quizzes = average(student[“quizzes”])
tests = average(student[“tests”])
return (homework * 0.10) + (quizzes * 0.30) + (tests * 0.60)

2 Likes

you got it wrong , you noob

1 Like

Can someone please tell me what is wrong with my code on this? I am getting an error from Alice’s weighted average and I don’t know why.
This is the error:

get_average(alice) returned 151.1833333333 instead of the expected 91.15

Below is my code:

lloyd = {
“name”: “Lloyd”,
“homework”: [90.0, 97.0, 75.0, 92.0],
“quizzes”: [88.0, 40.0, 94.0],
“tests”: [75.0, 90.0]
}
alice = {
“name”: “Alice”,
“homework”: [100.0, 92.0, 98.0, 100.0],
“quizzes”: [82.0, 83.0, 91.0],
“tests”: [89.0, 97.0]
}
tyler = {
“name”: “Tyler”,
“homework”: [0.0, 87.0, 75.0, 22.0],
“quizzes”: [0.0, 75.0, 78.0],
“tests”: [100.0, 100.0]
}

Add your function below!

def average(numbers):
total = sum(numbers)
total = float(total)
return total / len(numbers)

def get_average(student):
homework = average(student[“homework”])
quizzes = average(student[“quizzes”])
tests = average(student[“tests”])

total = homework * .1 + quizzes + .3 + tests * .6
return total

it shouldn’t be total