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


#1



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?

https://www.codecademy.com/courses/python-beginner-nzzVa/2/3?curriculum_id=4f89dab3d788890003000096


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

print list_function(n)


#2

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


#3

i i run your code it gives 8:

as expected


#4

That is so weird.

Have a look


#5

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


#6

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)


#7

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


#8

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


#9

Ok. Thank you very much


#10

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