"result" not defined


Iterating over a list in a function

I am just following the instructions but no output.
This is the error message and code below. Not able to figure out the mistake.

Traceback (most recent call last):
File "python", line 7, in
NameError: name 'result' is not defined

n = [3, 5, 7]
def total(numbers):
    result = 0
    for i in range(len(numbers)):
        result = result + i
    return result
print result


Because result is only defined within your function, you cannot merely print it outside of the function. What you need to do is print total(n).


Simply change result to total(n) in your final print statement, and you should be good.


Thanks a lot!that worked


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