Iterating over a list in a function


#1



Iterating over a list in a function

<>

I ran through the forums and managed to get the code to work correctly. But i don't understand how does the function calls n list here?


i expected the function to run through the n list to add up to total?


n = [3, 5, 7]

def total(numbers):
    result = 0
    for i in range(len(numbers)):
        result = result + numbers[i]   #there is no reference to n here
    print result
    return result

`


#2

It is done by CC interpreter.
Even you're not calling this function,CC will show you the o/p's
(It automatically test function on different values)


#3

not entirely sure what you meant by that, but i guess it means that it is working as python intended :slight_smile:


#4

I mean the interpreter we use here ,Code-Cademy tests our program by automatically providing inputs to it.


#5

does that mean that if i use python (not the Code-Academy one here), it wouldn't work that way?

i tried using Python Idle, but it didn't generate any errors there. sorry to be anal here. just curious


#6

When you run codes on IDLE,You've to call your function and pass arguments to it.
But at CC's interpreter,It checks(many of the times) program by calling function at different values.(even you're not calling and passing any inputs)


#7

I was wondering the same thing. So in an actual Python application, how would:

n = [3, 5, 7]

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

look in something like IDLE? would "numbers" be replaced with "n"?


#8

Hi @eldiin1,

There is two things to keep in mind.

The variables that we define outside of a function are called to be in global scope.
They're called global variables.

The variables that we define inside the function used to be in local scope.
They're called local variables.

Note: Function can also use global variable or even you can define global variables inside the function.

lets understand it..

def Double(x):
    return 2 * x

def Double(y):
    return 2 * y

def Double(z):
    return 2 * z

I defined 3 functions and just changed the parameter's name.
Do you think they will work differently?
Nope.

Key thing to understand that the parameter you pass are in local scope.
They are just names,like variables. You can name it anything you like.(Make sure that they have valid variable's name). here in my example it represents any general number.

When you pass some value..

Double(4)

The parameter is just replaced by this value. (x --- > 4 )

One more thing.

These parameters or variables that're defined inside the function have existence till they're called and executed,after execution halts they have no meaning.

In your above code,the numbers parameter actually represents any list with numbers as item.

so if you pass n,it will work on it.

even you make your own list..

m = [23,4,5,6,7,8]

and pass it to your above code by calling the function,It will work.


#9

So the only way to get the code to work in IDLE is to pass n into the defined function? Such as:

n = [3, 5, 7]


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

#10

No,As I said your function can have any parameter.
In IDLE tho you need to call your function.
then pass n. (after you define n)


#11

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