Site giving me a "float object not iterable" when code runs without error


#1

I am working on the deviation function section and the code runs with out errors and giving me the correct answer, yet i received a "float object not iterable" from the site. Can someone let me know why this is?

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


def grades_std_deviation(variance):
    deviation = grades_variance(variance) ** .5
    return int(deviation)

V/R


#2

Do they mention how they produced that error? If so, do the same thing.

You also return var out of nowhere, you have not defined that.

If we're to try running your code, we would need the whole code, that won't run on its own.

You may have misunderstood the instructions about some function, and therefore your own tests fail because you test something else. So double check on that as well.


#3

Here is my all of my code. When i run it through pycharm or another python IDE, i get no error. The site isn't giving me an error in the command window. I'm getting a bubble pop up from the site that states "float object not iterable" at the bottom of the page. So I'm not sure what line it is referencing or what is "float object not iterable".

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 / float(len(grades))
    return average


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


def grades_std_deviation(variance):
    deviation = grades_variance(variance) ** .5
    return int(deviation)
    

print(grades_std_deviation(grades))

V/R,


#4

Can't run that, mangled formatting.

Did you compare the instructions for each function to your code?

And if you got this error message:

grades_std_deviation(1.25) raised an error: 'float' object is not iterable

Then it tells you when it crashes. Use that information.


#5

Please format your post as I have done (select entire code block; press Ctrl+Shift+C) from next time. Thanks! :smile:

I have checked your code and found that you have misinterpreted the instructions. Carefully read the instructions:

  1. Define a function grades_std_deviation(variance)
  2. return the result of variance ** 0.5

Now see what you have done:

def grades_std_deviation(variance):
    deviation = grades_variance(variance) ** .5
    return int(deviation)

Can you spot the problem?


#6

I apologize, I thought I did format it like my first post. Are there people that will format something that is not formatted on the site, because when i look at this page it looks formatted to me? Thanks for informing me on how to correctly format for the next time.

I re-read the instructions and you are correct, I overlooked the them. I was calling the previous function within the dev function, but that was not how it was wanting me to do it.

Thanks


#7

The moderators/admin can edit your posts, and you can always see which moderator/admin edited your post by clicking that pencil on the top-right of your post :smile: I formatted the code for you this time, but be sure to do so the next time yourself :wink: