Why won't this work


I am new to python and mostly fumble through these lessons.
I am trying to iterate over a list in a function using indexes.

This is topic 15 of lists and functions. There are two ways suggested to do this. This is the second way.

I get this error
File "python", line 7
SyntaxError: 'return' outside function

I expect it to up the numbers in a list.

n = [3, 5, 7]

def total(numbers):   
    result = 0
    for i in range(len(numbers)):
        result = result + numbers[i]
return total(n)


Read again the error message, return is outside the function. Please note that indentations are very important in Python.

And do you really want to return total(n) or maybe you want to return result?


OK, thanks. I returned the result with the function. That makes sense.
Then I ran the function and printed the result.


So problem solved? :slight_smile:


Yes, thanks. It's all about the indent and the return.


Awesome! You're very welcome :slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.