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)

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

2 Likes

you typed quizzes + . 3… it should be quizzes * .3

2 Likes

def get_average(student):

homework = average(student[“homework”])

quizzes = average(student[“quizzes”])

tests = average(student[“tests”])

return 0.10 * homework + 0.30 * quizzes + 0.60 * tests

My code matches the solution, but it keeps saying:

File “python”, line 32
SyntaxError: ‘return’ outside function

In order to proceed I have to replace my code with the exact same code from the solution.

Huh?!

Without seeing your code it’s hard to guess exactly what’s different. Consider using a site for posting code snippets (pastebins, gists or repls are all good) and/or have a read of How do I format code in my posts?.

The syntax error suggests that you’ve used the return statement outside the body of a function which is not valid syntax. Has some indentation gone walkabouts? Remember that the body of a function should be indented.

The code is exactly the same as the solution. Indents all in correct places. No word incorrectly spelled.

How did everyone work out the weighted averages to be 6 for tests, 3 for quizzes and 1 for homework?

One way to do that would be to use a weight of:
0.10 for homework,
0.30 for quizzes
0.60 for tests,
(changed the percents to decimals by dividing by 100%)

If you multiply the weights by 10 (and get 1, 3, and 6) and use that instead,
then you have to divide by the total of the weights (10) after doing all the additions to get the weighted average.

Thankyou for your reply!!! What I was trying to ask was, how did you work out that codeacademy, wanted you to dictate the weighted averages to be .1 for homework, .3 for quizzes and .6 for tests, (I understand it’s taken as a percentage, so the total of the three variables has to add up to 10) and not for example: .6 for homework, .3 for quizzes and .1 for tests? Did you click on the community help link, within the problem, that takes you to this page, where it tells you the values at the top?

yes.
Although I think the stuff might work if you use any percentages that add up to 100%.

Ah okay, thankyou for clearing that up!!

Just an added note, weighted averages will always add up to unity (1). This is where we get the concept of weights.

Here is my code, and it’s not printing anything. Help what am i doing wrong?

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

def get_letter_grade(score):
if score >= 90:
return “A”
elif score >= 80:
return “B”
elif score >= 70:
return “C”
elif score >= 70:
return “D”
else:
return “F”
print get_letter_grade(get_average(lloyd))

I tried your code, and it seems okay.
Check your indentations,
and the last 70 should be a 60.

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 def get_letter_grade(score): if score >= 90: return "A" elif score >= 80: return "B" elif score >= 70: return "C" elif score >= 70: return "D" else: return "F" print (get_letter_grade(get_average(lloyd)))

I put ( and ) for the stuff print is “called” on so that this works in Python 3.

That was it!! I was following a python2 tut.Thank you!

multiply each number by its weight, then add the results . If the weights don’t add up to one, find the sum of all the variables multiplied by their weight, then divide by the sum of the weights.

Solution:
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