Why won't this work


#1



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)


#2

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?


#3

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


#4

So problem solved? :slight_smile:


#5

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


#6

Awesome! You're very welcome :slight_smile:


#7

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