Iterating over a list in a function


#1



What am i doing wrong?


n = [3, 5, 7]

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


#2

You should add the number to the result like so:

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

In your code, your result of addition is never assigned to the result variable.


#3

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