15. Iterating over a list in a function


#1

n = [3, 5, 7]

def total(numbers):
result = 0

for i in range(len(n)):
    result = result + i
return result

it id giving me error like "
Oops, try again. total([0, 3, 6]) returned 3 instead of 9"


#2

You're iteration over the range of the length of the n list. range(3) will iterate over the numbers 0, 1 and 2. Therefore you return the total of 0+1+2.

In this question you're asked to iterate over the list n. So: for i in n .


#3

but i wanna use range
then what should i do ?


#4

Why do you want to use range?

You're calculating the total of the values in n. For this you need the values in n, if you use the range of the length of n you're not using the values in n and unable to calculate the total...


#5

n = [3, 5, 7]

def total(n):
result = 0

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

this also runs
using Range


#6

My previous version of this post was wrong, it can work with range. While writing the first version of this post I somehow managed to miss the obvious n[i] and mistook it for a plain i like in the code first posted in this topic.


Analysis on the code that was first posted.:

n = [3, 5, 7]                     #step 1 

def total(n):
    result = 0                    #step 2

    for i in range(len(n)):       #step 3
        result = result + i    #step 4
    return result

This code step by step:
1) The list n is defined, with the contents: 3, 5, 7

2) The var result is defined, with the value: 0

3) A for-loop which iterates over each value in the range of the length of list n
The lenght of list n is 3, because the list contains three values (3, 5, 7)
We're iterating over the range of the length, thus we're iterating over the range of 3.
The numbers included in this range are: 0, 1, 2.
So we're iterating over the values 0, 1, 2.

4) Inside the loop we take one of the values, and add this to the var result. So:

i = 0 -> result (0) = result (0) + 0
i = 1 -> result (0) = result (0) + 1
i = 2 -> result (1) = result (1) + 2         -> result is now 3

So we've come to the total of 3, but the total should be 15, because 3 + 5 + 7 = 15.


Now let's take a look at the loop that doesn't use range:

n = [3, 5, 7]                     #step 1 

def total(n):
    result = 0                    #step 2

    for i in n:                   #step 3
        result = result + i    #step 4
    return result

This code step by step:
1) The list n is defined, with the contents: 3, 5, 7

2) The var result is defined, with the value: 0

3) A for-loop which iterates over each value in the list n.
So we're iterating over the values: 3, 5, 7

4) Inside the loop we take one of the values, and add this to the var result. So:

i = 3 -> result (0) = result (0) + 3
i = 5 -> result (3) = result (3) + 5
i = 7 -> result (8) = result (8) + 7         -> result is now 15

So we've come to the total of 15, this makes sense because: 3 + 5 + 7 = 15.


I hope this step-by-step explanation helps.


#7

I use this.
n = [3, 5, 7]

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


#8
n = [3, 5, 7]                     #step 1 

def total(n):
    result = 0                    #step 2

    for i in n:                   #step 3
        result = result + n[i]    #step 4
    return result

I'm noticing as I test this code the problem seems to lie within adding n[i] to result. I think it's trying to add the value of the '3rd index' to result, which would be n[3] based on this. But there is no 3rd index, we only have 0, 1 and 2, Which also fall under range(0, len(n)). If you switch that portion to:

for i in n:
    result = result + i

This clarifies the issue because now it will add 3 to result, instead of index 3 of n.

What I'm thinking is that when you loop strictly through a list, i becomes the value of each index.
I.E: First loop means that i will be the 0th index of a list. Second loop will be the first index, and so on.

But when you loop through a range of 0 to the length of a list, i becomes 0, and then 1, and then 2. So you need to adapt the code to accommodate this change.

I'm not sure if this is right or not. It's just what I'm theorizing as I test all the codes you guys have posted.

Sorry, I'm just re-iterating it to myself on text to feel like I'm understanding it or not.


#9

That is correct, I have absolutely no idea how I missed that nine days ago....


#10

thank you so much :smiley::smiley::smiley:


#11

This code doesn't work for me.

I just used shorthand

result += n[i]

instead but shouldn't matter.

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