Just Weight and See: Issues with averages


#1





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


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


Having trouble getting averages. Help?

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


#2

a function ends the moment a return keyword is reached

so you first need to calculate the weighted average, then return this weighted average


#3

Okay, so I tried moving return towards the end, however am now having a new problem. This error appears:

Oops, try again. get_average(alice) returned function get_average at 0x7f3c3b1ae848 instead of the expected 91.15

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


#4

but to get the weighted average, you need to add 0.1 * average(student["homework"]), 0.3 * average(student["quizzes"]) and 0.6 * average(student["tests"]) together, then return the weighted average you just calculated


#5

Oh right, just looked at the example again, didn't see the plus sign.


#6

here is my code:

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

also i've tried this:

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

i get the error: 
 File "python", line 30
    avg_sum = 0.1 * average(student["homework"]) + \ 0.3 * average(student["quizzes"]) + \ 0.6 * average(student["tests"])
                                                                                                  ^

SyntaxError: unexpected character after line continuation character

the error is shown on both codes of course
whats wrong here?


#7

you use backslashes, which is used to continue coding on a new line, but in your case the code is on the same line


#9

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