Modifying an element of a list in a function (clarification question)


Hi. I just completed this exercise. The console says [3, 11, 7] which is apparently correct. I just don't understand why 11 is the correct answer. I modified index 1, which was 5, to add 3 to the value. 5 + 3 is not 11. I don't get why 11 is the correct? Could someone clarify that for me?

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

print list_function(n)


Is it because the value x[1] is equal to itself times two and then 3 added to it?


i i run your code it gives 8:

as expected


That is so weird.

Have a look


Ah. I found the error.. I don't know why list_function(n) is there


you call list_function twice, this will first increase 5 to 8, and then 8 to 11

n is your function argument, which is then copied into your function parameter (x)


Hi. I can't seem to understand why we need "return x" in the program


because we want that our function gives us back the modified list


Ok. Thank you very much


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