Iterating over a list in a function


Hello everyone.How Could i use append in this task?Is it possible

Replace this line with your code. 

n = [3, 5, 7]

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


What do the instructions ask for? The above code looks a bit off. Post a link to the lesson so we can brush up.


I don't know if you can append... I didn't



Create a function that returns the sum of a list of numbers.

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.

result = 0

That varialbe will be used to accumulate the total, so we will not use it as an iterator variable in the loop.

for n in numbers:
    result += n

Be sure that return is not insde the loop.

return result


ok thanks a lot.It was very helpful


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