SOLVED 6. Just Weight and See


#1



https://www.codecademy.com/courses/python-beginner-en-qzsCL/1/2?curriculum_id=4f89dab3d788890003000096

It shows this error code:
Oops, try again. get_average(alice) returned 227.4 instead of the expected 91.15

I am not sure why it's not working.


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
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"]) 
    tests = average(student["tests"]) 
    return homework * 0.1 + quizzes * 0.3 + tests * 0.6


Thanks so much :slight_smile:


#2

When you average something you need to take the sum of all parts and then divide by the total parts.

So

a_list = [1, 31, 21, 91, 35, 55]
total = sum(a_list)
print("Average :{:.2f}".format(total / len(a_list)))
# OUTPUT: Average :39.00

If you have any questions feel free to ask.


#3

I have done this to my code because I have to multiply the 3 averages by their weights and return the sum of those three. Homework is 10%, quizzes are 30% and tests are 60%. BUT IT STILL DOESN'T WORK AND I DON'T KNOW WHY!!!!

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
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"]) 
    tests = average(student["tests"]) 
    homework = homework * 0.1
    quizzes = quizzes * 0.3
    tests = tests * 0.6
    return homework + quizzes + tests

Thank you for helping me! :slight_smile:


#4

Owen, a.k.a. @zeziba, has perfectly explained how average works and if you pay close attention to your function average, it only brings back a sum of values but not it's average.

Hope it helps you :slight_smile:


#5

Thanks so much!!! :slight_smile: