Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I can't sum the numbers of a nested loop. TypeError: 'float' object cannot be interpreted as an integer

I would like to sum all the item elements and get the result 18, but I get the error:

TypeError: 'float' object cannot be interpreted as an integer

The problem is that I cannot sum the values_subtraction_average values which are: 5.0, 0.0, -4.0, 4.0, -2.0, -2.0, -1.0. So I think the problem is the second loop.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The goal is to use the first loop to subtract each individual value from the mean and get 5.0, 0.0, -4.0, 4.0, -2.0, -2.0, -1.0. Next, in the second loop, i would like to sum all the results of values_subtraction_average and get the result 18. How can i solve the problem? What line of code would I need?

Code:

values = [29, 24, 20, 28, 22, 22, 23]
average = sum(values) / len(values)

for single_value in values:
    values_subtraction_average = single_value - average
    # >>> 5.0, 0.0, -4.0, 4.0, -2.0, -2.0, -1.0.

    for item in range(values_subtraction_average):
        x = sum(float(item))

>Solution :

Try:

values = [29, 24, 20, 28, 22, 22, 23]

x = sum(values) / len(values)
y = sum(abs(v - x) for v in values)
print(y)

print(y / len(values))

Prints:

18.0
2.5714285714285716
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading