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.
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
