Iterating over a list in a function


Oops, try again. total([0, 3, 6]) returned 3 instead of 9

expected it to work oviusly

[3, 5, 7]

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

print total(n)


You're only adding the indices(0, 1, 2) to result but not the actual items in the list.



result += x

This should be:

result += numbers[x]


The problem is your list doesn't have a name,

store it in a variable and your code should work. :slight_smile:


nope....just didnt copy the name


but i didnt get the diffrence.


okay then your problem is your loop,

you don't need range and len simply write,

for x in your_list_name #change the value of your_list_name:


@bandit, what are you talking about? While I agree there appears to be a missing n = at the start of the code, the main issue here was to do with adding to result within the for loop.


That was not the issue,

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

The code above passes the SCT, which is what I advised @webmaster46924 to do.


Weird, because this is my code...

n = [3, 5, 7]

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

And @webmaster46924 appeared to be going down this route.


well actually the problem was that i added x to result insted of numbers[x]

the other problem is that i dont understand the diffrence between the 2


Ohhhh so your argument is that the solution I gave him was not related to his initial code?


To a certain extent, yes.


Noted @aquaphoenix17 :slight_smile:


To understand why we use numbers as we do, you must first understand why we use parameters.

As of defining the function, the program does not know whether numbers is a number, a string, a list, a dictionary, etc. As far as the program is concerned, numbers could be anything, and the the program could care less. It's just a placeholder for whatever you pass into the function when you actually call it. In other words think of parameters like variables, whenever you call a function, the argument you pass into it becomes the value for numbers. For instance if you called:


You would essentially be telling the program to run total(numbers) except now, numbers = n. Until a function is actually called, the parameters you have set are valueless placeholders. Once you call the function and pass in arguments for those parameters, they take on value and purpose. What I mean by this is, if you did the following:

n = [3, 5 7]

def total(numbers):
    result = 0
    for x in range(len(numbers)):
        result += numbers[x]
    return result


Notice how I defined the function using numbers as a parameter and then I called the function using n as an argument. This is telling the program to run the code within total(numbers) but replace all mentions of numbers with n. Basically, it does this:

result = 0
for x in range(len(n)):
    result += n[x]
return result

n[x] is an identifier for the value of x in the list called n.

Without specifying a list and using x as an identifier, the program would just add the numbers 1, 2, and 3 to result since that is the length of n. That is not what we want.

This is why we use numbers as we do in the function.


so basically
x is the length of the list
n[x] is the items in the list



In the context of this function, that is correct.


thanks a lot! you really helped me


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