Iterating over a list in function, Oops, try again. total([0, 3, 6]) returned 0 instead of 9


n = [3, 5, 7]

def total(numbers):
    for i in range(len(numbers)):
        print numbers[i]
        result= result+ numbers[i]
        return result
# Don't forget to return your new list!


a function ends the moment a return keyword is reached

if a return keyword is reached in a loop, the loop will break


can you write and show how it should be done?


you should remove return from the loop, so the whole loop can run

currently your loop breaks in its first iteration


cool i got it the result.


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