Float object is not iterbale


#1

I need some help figuring out where I'm going wrong:

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def print_grades(grades):
    for grade in grades:
        print grade

def grades_sum(grades):
    total = 0
    for grade in grades: 
        total += grade
    return total
    
def grades_average(grades):
    sum_of_grades = grades_sum(grades)
    average = sum_of_grades / len(grades)
    return average

def grades_variance(scores):
    average = grades_average(scores)
    variance = 0
    for score in scores:
        variance += (average - score) ** 2
    return variance / len(scores)
        
def grades_std_deviation(variance):
    return grades_variance(variance) ** 0.5
    
variance = grades_variance(grades)
print grades_std_deviation(variance)

#2

If you reproduce the crash yourself then you'll have yourself a line number for where it's happening.
Also make sure you understand what that message means - if you don't then that's something to google for, or ask (but be specific)


#3

I understand what it means, but I don't see where the it stems from.
The error is:

Traceback (most recent call last):
  File "python", line 29, in <module>
  File "python", line 26, in grades_std_deviation
  File "python", line 19, in grades_variance
  File "python", line 14, in grades_average
  File "python", line 9, in grades_sum
TypeError: 'float' object is not iterable

#4

That says where though.
Go there and consider what you meant to do, investigate the values you're using / or maybe you didn't mean to do iteration and should have done some other action at that point in time


#5

I see that it says where, but I am having trouble figuring it out. All of my functions worked before and now the error is tracing back through all of them.


#6

The first line says "most recent call last"

So the place where it crashed is the last location listed.

It says that you're iterating through a float - which isn't possible. So you would need to look at that location and decide on whether it should be doing something else, or if it should be operating on some other data. For example, one of the functions that called it may have given it bad arguments.

Identify the bad pieces and keep following where they come from.


#7

It seems to me that you are return a number out of a function that isn't correct.

def grades_variance(scores):
    average = grades_average(scores)
    variance = 0
    for score in scores:
        variance += (average - score) ** 2
    return variance / len(scores)
        
def grades_std_deviation(variance):
    return grades_variance(variance) ** 0.5

you want to return variance in grades_variance function like this:

def grades_variance(scores):
    average = grades_average(scores)
    variance = 0
    for score in scores:
        variance += (average - score) ** 2
    variance = variance / len(scores)
    return variance

def grades_std_deviation(variance):
    return variance ** 0.5