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

#1

no idea why it keeps getting stuck at this part.

``````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]
}
students=[lloyd,alice,tyler]
def average(numbers):
total=sum(numbers)
average=float(total)/len(numbers)
return average
def get_average(student):
for student in students:
homework=0.1*average(student["homework"])
quizzes=0.3*average(student["quizzes"])
tests=0.6*average(student["tests"])

get_average=homework+quizzes+tests
return get_average``````

#2

Why going trough the `students` list when you have the exact student in the function's parameter? (`get_average(alice)`)

The logic is fine, I think it messed up when you returned the result still inside the `for` cycle.

#3

what i tried to do is to use a for loop to compute the score of all 3 students at once. not just alice.

i shifted the return result to out of the for loop. but now the answer is 79.9... still wrong

#4

i somehow managed to solve the issue with the following code:

def get_average(students):
for student in students:
homework=0.1*average(students["homework"])
quizzes=0.3*average(students["quizzes"])
tests=0.6*average(students["tests"]
get_average=homework+quizzes+tests
return get_average

but i dont really get the logic, anybody can to explain??

#5

You could go trough the students list if you wanted, yes, but that's not what the instructions are asking for.

Write a function called `get_average` that takes a student dictionary (like `lloyd`, `alice`, or `tyler`) as input and `return`s his/her weighted average.

In further lessons they'll ask you to define a new function that calculates the average of the whole class, you're a few steps ahead already.

#6

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