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


#1



n = [3, 5, 7]

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


#2

a function ends the moment a return keyword is reached

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


#3

can you write and show how it should be done?


#4

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

currently your loop breaks in its first iteration


#5

cool i got it the result.


#6

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