6/9 6. Just Weight And See - 'float' not iterable error?


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

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

ERROR: Oops, try again. get_average(alice) raised the following error: 'float' object is not iterable
CONSOLE: None

What is the error saying I'm doing wrong?!


#2

A sum is computed out of multiple terms. You're doing
sum(7.0), when sum tries to iterate through the argument you gave it, then it will be trying to iterate through a float, which is not possible


#3

I'm confused... so sum() can't add floats and only integers?


#4

You can add floats. But you can't compute the sum of a single number, that doesn't make sense.

You can compute the sum of a collection of numbers, which may consist of just one number.

You are not using sum to add anything, you are adding with +

sum(5) # doesn't make sense
sum([5]) # 5
sum([4, 3]) # 7

#5

oh, i see!
Thanks for the help! :smiley:


#6

so basically, all he needs to do is to delete the "+" ?


#7

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

You can also alter your code to first calculate sum and store it in a variable. Just return that variable.