Iterating over a list in a function


#1



https://www.codecademy.com/courses/python-beginner-nzzVa/3/4?curriculum_id=4f89dab3d788890003000096


don't know how to make the total. Thanks for reading
here are the instructions
On line 3, define a function called total that accepts one argument called numbers. It will be a list.
Inside the function, create a variable called result and set it to zero.
Using one of the two methods above, iterate through the numbers list.
For each number, add it to result.
Finally, return result.
Create a function called total that adds up all the elements of an arbitrary list and returns that count, using the existing code as a hint. Use a for loop so it can be used for any size list.


n = [3, 5, 7]

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


#2

When iterating over a range, the iterator is an integer index, not an item value.

result += numbers[i]

#3

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