Just Weight and See: code worked, but wrong answer for Alice


It looks like my code works, but it returns the wrong answer for student alice

Here is the complete 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]

from math import *

def get_average(student):
    for key in student:
        if key=="homework":
        return avg_homework
    elif key=="quizzes":
        return avg_quizzes
    elif key=="tests":
        return avg_tests
return 0.1*avg_homework + 0.3*avg_quizzes + 0.6*avg_tests
print get_average(lloyd)
print get_average(alice)
print get_average(tyler)

No error message shows up for the code itself, and it prints out the average values for the three students:

But the print-out value for student alice is not what the system expects, it shows this:

Oops, try again. get_average(alice) returned 85.3333333333 instead of the expected 91.15

Can anyone explain why I got the wrong answer for alice?
BTW, I did the indentations right.



You should not be returning inside the loop.


Thanks for replying, but i didn't quite get you. If i delete the return part, the code doesn't work at all. At least, my original code works, with the "return" part.


Have you checked the indentation? What errors are being raised?

Can't help but notice that none of this code is in the instructions. Is this something you are trying to do on your own? If so, then let's move it into the Corner Bar where it can be openly discussed outside of the lesson targeted Q&A.


The indentation should be right. No errors showed up for the code itself, it just said that: get_average(alice) returned 85.3333333333 instead of the expected 91.15

I did this in order to solve the problem raised in the instrcution. I was trying to get the average scores for homework, quizzes and tests first, and then multiply the 3 averages by their weights and return the sum of those three. Homework is 10%, quizzes are 30% and tests are 60%.

how can we move it to the Corner Bar? thanks.


We only have to move it if it is off topic, not directly related to the lesson. If this is your attempt at following the instructions, then the thing to do is go back to the start, and rethink your approach. It does not require anything as complicated as this. Think simple. You know what you want and will expect, now just come up with a much simpler way to get that.


Thanks, I just solved it in a simple way.