11/18 list manipulations in functions

I’m very confused as to why this isn’t working. Here’s my code:
n = [3, 5, 7]

Add your function here

def list_extender(lst):
n.append(9)
return lst

print list_extender(n)

I’m getting this error:
Oops, try again.
list_extender([1, 2, 3, 4]) returned [1, 2, 3, 4] instead of [1, 2, 3, 4, 9]

but on the console it’s printing
[3, 5, 7, 9]
None

It seems like I’ve done everything correctly?

Hi instead of n.append(9) Try that lst.append(9)

That worked, but I don’t get why. Aren’t I supposed to be appending n not lst? How does it know to add 9 to n when I tell it to append lst?

Thanks!

I think that its because the function list_extender append the number 9 and then when we call it with the the n list Its add the number 9

‘lst’ is an example list which stands for any list that the function could receive. You give it a list when you call it ( e.g. list_extender(n) ). The function basically ‘replaces’ the example list with the list you’ve given it and does with it what you have programmed it to do (append 9).
Hope this helps :slight_smile:

I think I’m getting it, many thanks to you and wizmarco.

hi, i tried the same code with the modification that wizmarco has suggested, but the error message i get is:
File “python”, line 5
SyntaxError: ‘return’ outside function

Hi can you post your code ?

here how to formate your code

(n = [3, 5, 7]

Add your function here

def list_extender(lst):
lst.append(9)
return lst

print list_extender(n))

Hi it should be like that with the right indent

n = [3, 5, 7]

#Add your function here
def list_extender(lst):
    lst.append(9)
    return lst

print list_extender(n)


and here

print list_extender(n))

remove one ) after (n)

1 Like

thx srry to put u through the trouble, I also solved it earlier than your reply but it helps just to know that there is a slightly different answer but srry again for not noticing your reply and once again thank you

1 Like

n = [3, 5, 7]
def list_extender(lst):# Add your function here
lst.append(9)
return lst

print list_extender(n)

you have a list n[3,5,7] and print list_extender(n) this will print the List n.
then the function you define will add 9 at the end of any list, if you CHANGE the “return lst” to “RETURN n” look what will give you and try to anderstand why!!!

PLEASE DO THAT BEFORE RUNING IT.