6. Just weight and see


Oh wonderful. Up until this lesson things have been alright, but I just straight up don't know what a weighted average is. I've never even heard about it in school.

Anyone know what a weighted average is? I tried to wikipedia it, but I wound up smashing my head against my monitor instead.

so I've gotten this far

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)
    total = total / len(numbers)
    return total
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])

Then it asks you to multiplay the averages by their weights and my progress just comes to a complete halt.



tests = average(student["tests"]

missing closing parentheses at the end


@ steim94 oh whoops that was a just a typo in this post, ill fix it here.

I'm really wondering about the weighted averages part.


well, you know have 3 variables:


of which you can take a weighted average. the exercise gives a good hint on how to do this:

cost = {
    "apples": [3.5, 2.4, 2.3],
    "bananas": [1.2, 1.8],

return 0.9 * average(cost["apples"]) + \
0.1 * average(cost["bananas"])

apples weigh 90%, while bananas weigh 10%

except you have 3 things to calculate an average of, and the percentages might be different, and you already stored the average into variable which you can use


that's the thing though, where did they get the 90% for apples and the 10% for bananas?

all they say is "Since we like apples much more than we like bananas, we weight the average cost of apples by 90% " but there's nothing before that tells me how they got the 90%


it is just an example of how to calculate weighted average. This could have been any value, given it serves as an example

the percentage for how much the homework, quizzes and tests are, are given in the instructions. Just use those percentages.


Oh, man...

You're right. there they were right in front of me. I think I should take a break for a day. Don't know how I missed that.

Really appreciate the help, man.


always read till the end.

lets say i have the following numbers (they are just random):

a normal average would be: (100 + 90 + 80) / 3 = 90

makes sense, right? in this case, all numbers are evenly important.

you can also write this like:

100 * 0.333 + 90 * 0.333 + 80 * 0.333

see? now the weight of each grade is 33.3333... percent. perfectly balanced

if you have an weighted average not all numbers are evenly important. certain numbers have more influence: (again, random numbers), just for demonstration

100 counts for: 40% of the total grade
90 counts for 25% of the total grade
80 counts for 35% of the total grade:

(100 * 0.4 + 90 * 0.25 + 80 * 0.35 ) = 90.5 (so weighted average can influence your grade)


Hmm, interesting.

Now that I think about it, I remember one of my school teachers doing something like that to make sure we took a particular test more seriously.
Least I think that's what it was used for.

Thanks for explaining that.


yes, or if you give a presentation teachers also us weighted average to reach the final grade for your presentation, since some factors are more important in presentation and some are less important. factors could be: attitude, content quality, clearly speaking and so on


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.