Reputation: 183
I am new to python and learning from CodeAcademy.com; I have a problem:
Change list_function so that:
- Add 3 to the item at index one of the list.
- Store the result back into index one.
- Return the list.
Here is my code:
def list_function(x):
return x
n = [3, 5, 7]
n.insert(1,3)
print list_function(n)
I get only the error, what should i do?
My problem is to understand the number 2 and 3 option.
Upvotes: 0
Views: 404
Reputation: 1121436
You are confusing adding with inserting, point 1:
- Add 3 to the item at index one of the list.
You interpreted this as insertion:
n.insert(1,3)
but really they meant the arithmetic operation:
n[1] + 3
This adds 3 (with +
) tot the item at index one ([1]
) of the list (n
).
You then insert that back into the list at the same index:
n[1] = n[1] + 3
All this should be done inside your function:
def list_function(some_list):
some_list[1] = some_list[1] + 3 # step 1 and 2
return some_list # step 3
Upvotes: 2