FAQ: Learn Python - Lists and Functions - Iterating over a list in a function

True, but then you should still choice the best method of iteration depending on your use case

which you code? Your code? Or the solution? As I attempted to explain, your code and the solution code are not the same.

Solution only shows using range(len())
why is my result returning diff then if i used the range? it said either was fine
why do I keep getting error object is not iterable for this solution?
Do you have to use the word list in the for loop when iterating over a function or can it be something different?

n = [3, 5, 7]

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

alternatively

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

now I get the error list index out of range

first code sample:

for i in list:

list variable isn’t defined anywhere?

as for your issue, using for i in numbers gives you the values directly, you no longer need to access the list by index.

The reason you get an IndexError is because a list might have values higher then the highest index.

@josephill
i ran your first code and it didn’t work!!!

Hello @kommskmohamed6745559 and welcome to the Codecademy Forums!

This exercise uses Python 2, not Python 3. That may be why the code throws an error for you.

What am I doing wrong:

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

Item + result is not a legal operation.

If you meant to increment result variable then you need to check the item + result and redo it so it reflects something like

x += y 

so that when you return result its value is updated.

@psmilliorn do you know when to use ‘+’ versus using ‘+=’?

a = 0
b = 1
a = a + b

That is one example. You can also just do.

a = 0
b = 1
a += b

Hey guys, I’m really confused on the whole for loops being used in-conjunction with lists, don’t really feel previous lessons have explained it enough for me to understand. I was wondering if anyone could help provide an idiot proof explanation!!! Here is the code I have used:

n = [5,8,2]
def total(numbers):
   for n in numbers:
     result = 0
     result += n
     return result
print total(n) 

If I do the following then I get a result of 2:

   result += n
return result

When I run this code I get the value 5, and I don’t know why, however if I remove the function and just use a for loop like so:

n = [5,8,2]
for numbers in n:
    result = 0
    result += numbers
print result 

The code only prints out the last value in the list ( I know that the word numbers and n are swapped between the first and second example, and that the result += changes between n and numbers also, this is because if I don’t then on the second example I get an error saying int object is not iterable, I don’t entirely know what this means either.)

if I indent the print like so:

result += numbers
  print result

Then it ends up printing all 3 numbers ie:
5
8
2

If anyone could help explain what is happening and what I am doing wrong, I would me much grateful!!!

Please have a look at How do I format code in my posts? which explains how to keep the code formatting and for Python the indentation which is very important.

If I’m not mistaken your first code has the return statement inside the loop. As soon as return is executed the function stops at the point and returns the result to the original caller. If it’s in a loop this means your loop probably finishes too soon.

This is also what makes your print repeat three times. If you review the steps your loop takes you can probably also see why the output is 5, 8, 2 instead of the expected total.

Try playing around this until it makes sense :slightly_smiling_face:.

But I don’t know what steps my for loop takes, to get the result that it does, which is why I was wondering if anyone could help provide an explanation as to what I am doing wrong, or how to do it correctly?

I thought I had, your problem is that you return too early (and reset your counter too). Things inside the loop will run on every iteration of the loop. Think very carefully about what you actually want to repeat in this loop.

Can you explain what the error message object is not iterable?
Can you also explain if below is done:

my_list = [1,9,3,8,5,7]
total = 0
for number in my_list:
  total += number
  print total

Then it prints out:
1
10
13
21
26
33
but if the print statement is not indented, the same as previous line above, it just prints out 33, is it because if its indented, it’s saying for every item in the list, add that item to total then print the current total, and repeat this process, but if that is correct, if the print statement is unindented it just prints the whole total: 33, can you explain this please?

If I do the following:

my_list = [1,9,3,8,5,7]
def add_my_list(total):
  total = 0
  for number in my_list:
    total += number
  return total
print add_my_list(total)

Then I get an error saying total is not defined, but if I do the below, then I get 33? why if I declare total = 0, outside the function it works, but if I don’t, I get an error?

my_list = [1,9,3,8,5,7]
total = 0
def add_my_list(total):
  for number in my_list:
    total += number
  return total  
print add_my_list(total)

The first code is correct, except you then try to use total outside the function as well:

print add_my_list(total)

total has a local scope (only exists within the function)

when you call the function, I think you want to provide my_list as argument

about the second code sample: bad idea, you have a global total, which means multiple function calls will add to the total of earlier function calls

1 Like

Surely if print add_my_list(total) is carried out it is part of the function, as it is set to zero below the line def add_my_list ? When I do print add_my_list(my_list) it does return 33 (which is correct), but how why does this work if when I created the function I used the argument total ie, def add_my_list(total)?

no, functions are a set of instructions which are executed later. Kind of like a todo-list, you can make a todo list (defining functions), but actually executing the tasks on the todo-list (calling functions) can happen later

parameters like total here:

def add_my_list(total):

act as placeholder until the parameters receive their actual values from arguments at function call.

So I understand that a function is a way of creating a to-do list, which then can be called later, but if I understand what you’re saying correctly about place holders , in the example: def add_my_list(total) total is just a placeholder value, which means it hasn’t actually been defined? If that is the case then how come, when you type def add_my_list(total) the total is a place holder for an argument, and you then set it to zero ie:

def add_my_list(tottal)
   total = 0

how come if you then were to call the function with total as the argument it wont work?
ie: print add_my_list(total) since you have defined total by setting it to zero and then incorporating it into a for loop, add if total is just a place holder, what’s the point in using place holder’s if you can just use the name of the variable you want to pass as the argument?
ie:

my_list = [1,9,3,8,5,7]
def add_my_list(my_list):
  total = 0
  for number in my_list:
    total += number
  return total
print add_my_list(my_list)

Which still works and prints the correct result 33.

Think of it more like this…

def add_my_list(a_list):    # a_list is the parameter
  total = 0
  for number in a_list:
    total += number
  return total

my_list = [1,9,3,8,5,7]
print add_my_list(my_list)  # my_list is the argument

Parameters are names in the local namespace that refer to positional arguments. One parameter, one argument. The argument is an object, the parameter is the name that object will be known by in local scope. It gets messy when we give the argument (object) and the parameter the same name, especially if we don’t fully understand scope.

It would appear in this conversation that one is clear on the scope in which total is defined (as well as a_list) which explains why we cannot access it outside of the function.

1 Like