# Not getting the right value despite checking my code

My Code:

``````from numpy.random import randint, seed

equal_distances  = 0

for i in range(5000):
seed(i)
distribution = randint(0,1000,10)
mean = sum(distribution)/len(distribution)

distance_above = []
distance_below = []

for val in distribution:
if val == mean:
continue #next iteration
if val < mean:
distance_below.append(mean - val)
if val > mean:
distance_above.append(val - mean)

above = round(sum(distance_above),1)
below = round(sum(distance_below),1)

if (above==below):
equal_distances +=1
```

What I expected to happen:
To get 5000 for the equal_distances variable

What actually happened:
I am getting 5007! More than the actual range!
``````

equal_distancesint (<class ‘int’>)

• actual + expected - 5007 + 5000

equal_distances is greater than what we expected.

``````
<!--Enter other details below: -->``````

Since you want to measure the total distance above and below the mean, following should be calculated outside the distribution for loop.

Code
``````from numpy.random import randint, seed

equal_distances  = 0

for i in range(5000):
seed(i)
distribution = randint(0,1000,10)
mean = sum(distribution)/len(distribution)

distance_above = []
distance_below = []

for val in distribution:
if val == mean:
continue #next iteration
if val < mean:
distance_below.append(mean - val)
if val > mean:
distance_above.append(val - mean)

above = round(sum(distance_above),1)
below = round(sum(distance_below),1)

if (above==below):
equal_distances +=1
``````
2 Likes

Thanks so much, Viktor, you are absolutely right. Indentation mistake strikes again!

1 Like