# Python Syntax Error Return or Indentation

I can't figure out the correct indention for the return call. I either get an error of 'SyntaxError: 'return' outside function' or Unexpected Indentation.

```
def fun(a, r):
sum, term = 0, a
print("1st term is: ", term)
sum += term
for i in range(2, 20 + 1):
term *= r
sum += term
print(str(i)+"th term is: "+str(term))
print("Sum of first 20 terms is: ", sum)
return sum
if __name__ == '__main__':
a, r = 6, 2
Sum_formula = a*(pow(r, 20)-1) / (r-1)
Sum_loop = fun(a, r)
print("Sum of first 20 terms using formula: ", Sum_formula)
if Sum_formula-Sum_loop == 0:
print('Both sum are same, calculation is correct!!!')
else:
print("There is some error in your calculation")
```

U9-Forward
answered question

### 2 Answers

Follow what the error says: `SyntaxError: 'return' outside function`

, that's the accepted answer to your question, to get what i mean, the working code for that part would be:

```
def fun(a, r):
sum, term = 0, a
print("1st term is: ", term)
sum += term
for i in range(2, 20 + 1):
term *= r
sum += term
print(str(i)+"th term is: "+str(term))
print("Sum of first 20 terms is: ", sum)
return sum
```

And entire (whole) code would be:

```
def fun(a, r):
sum, term = 0, a
print("1st term is: ", term)
sum += term
for i in range(2, 20 + 1):
term *= r
sum += term
print(str(i)+"th term is: "+str(term))
print("Sum of first 20 terms is: ", sum)
return sum
if __name__ == '__main__':
a, r = 6, 2
Sum_formula = a*(pow(r, 20)-1) / (r-1)
Sum_loop = fun(a, r)
print("Sum of first 20 terms using formula: ", Sum_formula)
if Sum_formula-Sum_loop == 0:
print('Both sum are same, calculation is correct!!!')
else:
print("There is some error in your calculation")
```

U9-Forward
posted this

```
def fun(a, r):
sum, term = 0, a
print("1st term is: ", term)
sum += term
for i in range(2, 20 + 1):
term *= r
sum += term
print(str(i)+"th term is: "+str(term))
print("Sum of first 20 terms is: ", sum)
return sum
if __name__ == '__main__':
a, r = 6, 2
Sum_formula = a*(pow(r, 20)-1) / (r-1)
Sum_loop = fun(a, r)
print("Sum of first 20 terms using formula: ", Sum_formula)
if Sum_formula-Sum_loop == 0:
print('Both sum are same, calculation is correct!!!')
else:
print("There is some error in your calculation")
```

Shukla
posted this

## Have an answer?

JD

The body of your function needs to be indented. Right now everything after

`sum += term`

is outside of the function, but I think the`for`

loop is supposed to be in the function. See stackoverflow.com/a/45621723/354577