# calculation of code is not right and i am not able to find where i make mistake

I've written a program to calculate the standard deviation of a set of numbers. The program is running with no errors, however it is returning an incorrect result.

```
#standard deviation
import math
def mean(values):
return sum(values)/len(values)
def stanDev(values):
length=len(values)
total_sum = 0
m = mean(values)
for i in range(length):
total_sum += (values[i]-m)**2
under_root=total_sum/(length-1)
return math.sqrt(under_root)
x=[1,2,4,1,2,42,12]
std=stanDev(x)
print(std)
```

With the current code, I'm getting an output of `3.3243075080628843`

, however using an online calculator, I'm getting a result of `14.993649449334`

for the same set of data.

### 1 Answer

Do you have the correct indentation in your for loop? The code you supplied should look like this:

```
length=len(values)
total_sum = 0
m = mean(values)
for i in range(length):
total_sum += (values[i]-m)**2
under_root=total_sum/(length-1) #this line is performed once, after the for loop
return math.sqrt(under_root)
```

ljden
posted this

## Have an answer?

JD