'float' object is not iterable


#1

I know why this error arises, but don't know how to get around it. This is my code followed by the error message:

CODE:

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 x in grades:
        total += x
    return total

def grades_average(grades):
    sum_of_grades = grades_sum(grades)
    average = sum_of_grades / float(len(grades))
    return average

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

def grades_std_deviation(variance):
    return grades_variance(variance)** 0.5

print grades_std_deviation(grades)

ERROR:
Oops, try again. grades_std_deviation(4.0) raised an error: 'float' object is not iterable

Is there something I need to use other than a for loop? How am I supposed to handle an input for both a list and a float?


#2

Why would you need to handle both?


#3

Because the whole time code academy has us building this to handle a list, and then at the end it tests it with both a list, and a single float. My current code handles lists just fine, giving the appropriate standard deviation.

So maybe, is there any way I can detect if it's just one float variable and turn it into a list prior to using it in my for loops?

Or am I just completely missing what needs to be done?


#4

None of your functions need to handle both floats and lists as input. Check what it's supposed to be. Your error message already says which it should be, which leaves the question of what that function should be doing.

You can check the type, but there's no way to convert list to float or the other way around, they don't represent the same type of information.