Bug or am I confused 15: Iterating over a list in a function


#1

Where is total([0,3,6]) being called. I've noticed some of these tasks will call a function it has you write but I'm missing how these functions get called.

This exercise came with the list n=[3, 5, 7]
My function added this list properly, but the error is for a different list that I never defined.
I've noticed this similar behavior a few other times in chapter 12, but I don't remember where, so am I missing something or is this a bug?

EDIT: I just realized that my for loop needs to be...
for i in numbers:
and that worked, I'm still confused how the function gets called, or is code academy doing that for me?


#2

You made it work? Good. Codecademy calls the function, since it needs to (how else to validate your code), but you can call it yourself as well if you like, a function call is simple the function name with the arguments:

total(n)

That is it, if you don't remember, i recommend the lesson where this is explained, if i am not mistaken this should already be covered


#3

Hi @blogmaster09847 ,

Though Codecademy does not always ask you to test your functions by calling them, it is good programming policy to do so. If you write a large program with lots of untested functions, you are likely to wind up with buggy code. In most Codecademy exercises, calling your function will not cause you code to be rejected, provided that you have not introduced an error. There is one notable case, however, where Codecademy will object to your calling one of your functions, namely the second-to-last exercise of A Day at the Supermarket.


#4

I am also having the same prob. Can u help me.

Here is the code:

n = [3, 5, 7]

def total(n):
    result = 0
    for i in range(len(n)):     < I am iterating correctly, right?
        print n[i]
    return result

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

What is the prob?


#5

You are iterating correctly, but then this line:

print n[i]

you just print the number in the list, rather then adding/count it to total