10/18 Modifying an element of a list in a function - what is wrong here


#1

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

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


#2

Instruction says:

Return the list.

Your function returns only the changed value. Return x :smile:


#3

only just worked it out myself. for some reason, they don't want the list n at all. the code they want is

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

or

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

:grinning:


#5

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

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

Try this one. For me it worked :smile:


#6

good advice..thanks factoradic..:grin:


#7

Shouldn't it work the same with
return n
instead return x?
If not, I don't understand
thank you


#8

yes, you can also write it as

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

or

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

:grinning:

The idea is that you need the variable (in this case n) to be the same all the way through the def function

What I was trying to say, was that there is a bug in the codecademy code checker.
The question asks for

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

The answer checker only wanted

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

****They may have fixed this bug now. I'm not sure.****


#9

Thank you Bentley_maniac!


#10

I just have a doubt why not x[1] but only x in:

x[1] = x[1] + 3
return x


#11

oh my god! i was stuck on this too! thank you for the help!


#13

n = [3, 5, 7]
def double_list(x):
for i in range(0, len(x)):
x[i] = x[i] * 2

Don't forget to return your new list!

return x

print double_list(n)


#14

The guide said: "When we pass a list to a function and modify that list, like in the double_first function above, we end up modifying the original list."

Why do i need to return x if the list is modified already?


#15

I have done the exact code that you put down but it doesn't seem to work. Yes, I did put the indents on.


#16

Same here...

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

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

Why doesn't it work?


#17

I'm sure you figured that out already, but you have both x's and n's in the first bit of your code. Should be just one of these.

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

So

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


#18

This is the proper way to do it:

n = [3, 5, 7]
def double_list(x):
for i in range(0, len(x)):
x[i] = x[i] * 2
return x
print double_list(n)


#19

watch out for the identation on:
return x
return x should be on the same line with:
for i in range(0, len(x)):
this is because the return function is not in the loop but a result of the loop.


#20

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

n = [3, 5, 7]

print list_function(n)


#21

Thank you

I was stuck here for 5 days. Only stupid answers out there.

My mistake was ''(0, len(n)):'' - and I corrected as you said to ''(0, len(x)):''

Thank you !!!


#22

Thanks, the indentation on return x was my issue.