10. Modifying an element


#1



def list_function(n):
    n[1] += 3
    return n

n = [3, 5, 7]
print list_function(x)

The above code did not work in the interpreter.

However that same code above susbtituting "n" for "x"


def list_function(n):
    n[1] += 3
    return n

n = [3, 5, 7]
print list_function(n)

Works just fine is there something i'm missing here?


#2

print list_function(n) is the function call (with a print statement), you want to pass the list into the function, your list is stored in a variable called n:

n = [3, 5, 7]

not in a variable called x. You can also pass the list directly:

print list_function([3, 5, 7])

not sure if the exercise likes that... But i hope it does help your understanding :slight_smile:


#3

thank you, i understand now, but i thought since the "test" case was passing its own variables in anyways, i didnt need to rely on the previously assigned list (n) since it was calling my function passing in its own vars like you mentioned ex: list_function([0,1])


#4

you have to past the value and it has to wok for any list.

def list_function(x):
x[1] = x[1] + 3
return x
n = [3, 5, 7]
list_function(n)
print n

the function (take the second value in n and add 3) assining x[1] to any list.
then in list_function(n) you tell apply the function to de list call " n"
you can change (return x) to (return n) in the program and you will see that it tell you "list_function([3, 3]) returned [3, 8, 7] instead of [3, 6]" the web page check to see if your program work for any list!!!

got it??