6. Weight and See. Need Help


#1



get_average(alice) returned 227.4 instead of the expected 91.15


I thought it would be the correct code. Instead it keeps giving me this error or something similar. This is the most common error. I have already spent over a hour on it and I need help.


Replace this line with your code. 
def get_average(student):
        homework = average(student["homework"]) * 0.1
        quizzes = average(student["quizzes"]) * 0.3
        tests = average(student["tests"]) * 0.6
        return homework+quizzes+tests


#2

i can see your full code?


#3

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

#4

in your average function:

float(total)/len(numbers)

nice division, but you don't store it anywhere? so here:

return total

total will be returned, it will contain the sum of numbers, not the average.


#5

I would like to say this:
The previous problem was solved in what appeared to be in a weird way. The code I put in last lesson was similar to this:

def average(numbers):
    total=sum(numbers)
total = float(total)
    float(total)/len(numbers) 
    return total

But I had to remove total = float(total) for it to pass the lesson. This was after extensive looking into the Q&A and how I had it before was, as some people said, correct.


#6

the indention of this line looks off:

total = float(total)

shouldn't it have the same indent level as the lines around it?

The division:

float(total)/len(numbers)

you still don't store it anywhere. Now you execute the division but you don't store it anywhere, so total will return the sum not the average.


#7

Figured it out with your advice. Thank you.


#8

The average function now looks like this.

def average(numbers):
    total=sum(numbers)
    float(total)/len(numbers) == total
    return total

#9

so now you check if float(total) divided by the length of the list equals total. Does that make sense if you want to calculate the average? I would store (re-assign) the result of the division in total


#10

Wait no that's not it
This is it

def average(numbers):
    total=sum(numbers)
    total=float(total)/len(numbers)
    return total

#11

then i take what i said back here:

because this:

is the correct code, good you figured it out :slight_smile:

do you understand, or do you have questions left?


#12

Nope, none. You have been very helpful. Thanks


#13

Wait I do understand, don't have any questions


#14

that is good to hear :slight_smile: You're most welcome :slight_smile:


#15