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


#1

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":
        avg_homework=sum(student["homework"])/len(student["homework"])
        return avg_homework
    elif key=="quizzes":
        avg_quizzes=sum(student["quizzes"])/len(student["quizzes"])
        return avg_quizzes
    elif key=="tests":
        avg_homework=sum(stduent["tests"])/len(student["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:
74.0
85.3333333333
51.0

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.

Thanks


#2

You should not be returning inside the loop.


#3

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.


#4

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.


#5

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.


#6

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.


#7

Thanks, I just solved it in a simple way.


#9