12/15 return outside fusion


#1

what am i doing wrong

n = [3, 5, 7]

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

error message syntax error: return outside function


#2

n = [3, 5, 7]

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

We can see there are some indenation issues. But there is an even bigger issue, which is the use of result as both an object and a variable. Consider the correct code that follows and compare to your own:

def total(numbers):
    result = 0
    for i in numbers:
        result += i
    return result

See the difference, and where your mistakes were?

We can write this using the index of numbers by incorporating range() in the for statement:

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

#3

thank you for the help