Help please with this little exercise, modifying an element of a list in a function


In the lists and functions section of the course, section 10 modifies an element in three number list and should return that list.

This throws up the error ' list_function([5, 2]) returned [5, 2] instead of [5, 5] ' but the display seems right which is [3, 8, 7], 3 has been added to the 1st element in the list.

I started up python on my computer, and ran it there and it runs without error with the same result. Can anyone explain why it's ok in a python application but not on Code Academy and maybe where I am going wrong with this.

Thank you for any help you can give.

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


the great thing about function is that they can do a task multiply times, lets see what happens when we want to increase the second element in another list:

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

ouch, its not working, do you see the problem?


Thank you for the reply but unfortunately, I have no idea what it means. Where has the list [5, 2] come from? How is it being increased? Sorry but I'm not really sure your reply makes what is wrong with the code clear. I think I've modified the 1st element in the list n correctly and I think I'm returning the whole list, it's being displayed as the modified list.

Again, thank you for your reply. :smile:


i just made a random list, to demonstrate that your function is not working correctly, the function should return [5,5] when we input [5,2] to the function

its not, as you can see the list is not being increased while it should

But having a function which is only capable of modifying one list is pointless, like i said, function should be able to repetitive tasks, in this case increasing the second element in any list by 3

the list we supply as argument on function gets passed along into the function parameter (x)

so to make the function work for any list we supply as argument on function call, we could use the function parameter inside the function, and not n, given n is what causes your function to only work for a single list

the other great thing about function is that we can put them in a separate script and then import them, if i where to import your function from your script:

from yourscript import list_function

i would get an error saying n is undefined.

When your programs grow in size, this is very useful to do


Ok, I have finally figured out what I was doing wrong, just in case this helps anyone else, I was working on the list n which was outside the function and therefore not editable inside the function instead of x which was the function parameter.

After reading your reply a few times, I figured out what I was doing wrong. Thank you once more and I hope everyone enjoys their coding.


Yep, my reply is packed full with information

see the value of function and the parameter now? :slight_smile:


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