# Just Weight and See (Undefined error)

#1

The following code outputs

get_average(alice) raised the following error: ‘float’ object is not iterable.<

But why?

``````# Add your function below!
def average(numbers):
total = float(sum(numbers))
length = 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)
``````

#2

`sum()` can calculate the sum of an iterable (like a list), if you just have numbers you need to add together, you can use `+`

#3

Sorry… I could not get you… a brief explanation pls…

#4

do you have an an iterable (a data type which can be looped over, for example a list)?

sum() is a built-in function which can calculate the sum of an iterable, but an iterable only. It can’t add integers/floats together, so should you use `sum()` here?

#5

@stetim94 what about the following edit?

``````def average(numbers):
total = 0
for number in numbers:
total += number
length = len(numbers)
``````

or which other option do I have to run this code error-free?

#6

yes, because in the previous exercise you used an iterable (list) as parameter. In this exercise, your parameter is not an iterable. `sum()` only works for an iterable.

you can’t just assume a built-in function works for all data types. That is an assumption you can not make.

If you don’t know how a built-in function works, the python documentation is a good place to find out

#7

why would you change the `average` function? The average function is fine, there you can use `sum()` given the parameter of sum (`numbers`) is a list, the problem is in `get_average`, where you try to use `sum()` and the parameter is not an iterable/list

#8

Oh… Thanks a lot… I got it… It’s so silly that I didn’t notice it even once…
Thanks a lot again…
I needed to use comma(,) instead of plus(+) in the sum() function. The fact is I did not look at this statement ever. I was just thinking of the sum() in average()

#9

or just not use `sum()` at all in get_average, and add the numbers together using `+`

why bother to convert the numbers to an iterable so you can use `sum()`? Its additional steps which are not needed

#10

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