Iterating over a list in a function


#1


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

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)



#2

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


#3

Here:

result += x

This should be:

result += numbers[x]

#4

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

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


#5

nope....just didnt copy the name


#6

thanks!
but i didnt get the diffrence.


#7

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:


#8

@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.


#9

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.


#10

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.


#11

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


#12

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


#13

To a certain extent, yes.


#14

Noted @aquaphoenix17 :slight_smile:


#15

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:

total(n)

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

total(n)

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.


#16

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

correct?


#17

In the context of this function, that is correct.


#18

thanks a lot! you really helped me


#19

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