15. Iterating over a list in a function


#1



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


    result = result += numbers[i]
                     ^
SyntaxError: invalid syntax

I don't know why formatting is not working for me in this post. I've puzzled over this for about 20 minutes now and after searching I'm no nearer a solution. I've tried altering my code to match what I've seen others use or just bits suggested in their posts and still I'm confused. If I take out "result =" and leave it at £result += numbers[i]" I get "Oops, try again. total([0, 4, 1, 5]) returned 5 instead of 10" and that just confuses me even more. Here is my understanding of the exercise broken down by line. n is a list of 3 items and they are at index (0, 1, 2). In my function "total" it stores "numbers" (is this telling Python that it will be actual numbers or is that for my reference?) Then I set a variable of "result" to "0". For every i (is that short for item?) in the range from 0 to the legnth of the range (3 in n) Add the number from the list to the result variable. Return the result (which is the total) Hopefully someone can point out what I'm confusing or understanding incorrectly because I refuse to just look for someone else's answer until it works. I won't learn anything that way.

n = [3, 5, 7]

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



#2

look at the following code:

i = 5
i = i + 5
i += 5

using i += 5 is a shorthand for i = i +5, you combine this with the shorthand

your function now only works for one pre-defined list, not for any list you pass into the function as argument


#3

I'm sorry I really don't understand. I'm not sure why but today seems to be one where I have to read over and over trying to understand. I'm going to fix the formatting on my post.


#4

which one do you not understand? My answer contains 2 issues (using shorthand and normal variable update) or parameter/arguments.

lets add a function call:

print total(n)

n is your argument, which get copied into your function parameter (numbers), so you should loop over numbers, so your function works for any argument you supply when calling the function not only for your list n


#5

If I use the shorthand I it works even if I get the wrong answer. However, the longer way tells me "invalid syntax". I don't understand why it throws that up if they are essentially the same.

I'm also confused by it giving me 3 from 0, 3, 6 and 5 from 0, 4, 1, 5 using the shorthand method. Confused as in I don't understand what the code is doing to get those numbers. If I can understand that I can see where I have gone wrong.


#6

can i see an updated version of your code?


#7

Sure. I still don't know how to format my first post and it's hurting me deep inside.

n = [3, 5, 7]

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

#8

the exercise is going to call the function multiply times:

n = [3, 5, 7]

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

print total([1,2,3])
print total([4,5,6])

each time, with a different list. a function which only works for a single list, kind of sucks. Make sure your function works for each list supplied as argument

here:

result + numbers[i]

now you never use update result, either use = .. + .. or the shorthand += (but not both at the same time)


#9

I see part of what you're saying. That I can use "result = result +" or "result +=" but not both however that doesn't change anything for my code as I've tried three different ways.

If I use
result = result + numbers[i]
or
result += numbers[i]
I get the error "Oops, try again. total([0, 4, 1, 5]) returned 5 instead of 10 "

If I use
result + numbers[i]
I get the error "Oops, try again. total([0, 3, 6]) returned 0 instead of 9"

I understand that you're saying this function will only work for a set list and that's fine because that is the first part of the exercise. I don't want to create a function that uses any list given to it until I understand the first part. Right now I'm still confused why it's giving me these numbers or how to correct it.


#10

but your function needs to work for multiply lists, since the exercise is going to validate that your function works for multiply list. You simply have no choice in the matter.


#11

Ohhhhhh..... I ....... yeah I get it now. I think. You're saying that my code is working for the first list (n) but since the exercise wants me to create a function for any list given (and I haven't done that) it gives me the error when it passes any other list to it? If that is what you're saying then I understand and feel stupid.


#12

yes, you understand now :slight_smile: Now you only have to code this


#13

I'm sorry it took so long for my brain to understand. Last night it wasn't working and now it's just slowly starting to work. Thanks for the pointers regarding += or = ... + too. I will remember that.


#14