# Modifying an element of a list in a function

#1

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

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

The output is correct but the codeacdemy editor throws the following error:
Oops, try again. list_function([3, 5]) returned None, did you remember to return the result? Thanks in advance for your help.

#2

Writing return isn't enough. You have to write something like:

``````my_function(x):
return 2*x``````

Specify what you want to return. The reason your print statement does print something is that you don't print what is returned. Test your function with

`print 'n =', list_function(n)`

to see what your function returns for n.

#3

Thanks for you prompt response. I do not have any problems with my print statement. I get the following in the output window:

n = [3, 5, 7]
None

I have changed the code according to your suggestion

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

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

Then I get the following output:
n = 8
None

This is not correct. When I change the print statement back to print 'n =', n then I get a correct response. The problem is with the codecademy editor - it throws Oops, try again. list_function([2, 1]) returned 4 instead of [2, 4]

#4

You don't need to complicate it,

Just return the value of x,

`return x`

And for lack of confusion it helps to nest your print statement in parentheses,

`print ('n =', list_function(n))`

#5

Here you go - as you suggested:
def list_function(x):
x = x[1]+3
return x

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

Output is wrong - ('n =', 8)
None

codeacdemy editor error: Oops, try again. list_function([4, 5]) returned 8 instead of [4, 8]

#6

Call your function once simply like this,'

``print list_function(n)``

And make sure all your code is indented this way,

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

n = [3, 5, 7]``````

#7

#8

How did you get this to work. Mine fails:

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

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

Traceback (most recent call last):
File "python", line 6, in
File "python", line 2, in list_function
TypeError: 'int' object has no attribute 'getitem'

#9

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

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

I get
8
None
Oops, try again. list_function([6, 1]) returned 4 instead of [6, 4]

What am I doing wrong?

#10

Ignore, Got it figured out

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

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

#11

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