List manipulation in functions: 11


#1



n= [1,2,3,4]
# Add your function here
def list_extender(lst):
    n.append(9)
    return n
print list_extender(n)


I cannot understand what is wrong: It seems simple and I got the correct output right screen but it gives the error: Oops, try again. list_extender([1, 2, 3, 4]) returned [1, 2, 3, 4, 9, 9] instead of [1, 2, 3, 4, 9]

Can you please explain?


#2

You want to manipulate the list that's being sent as a parameter (lst), append a number to this list and return this list.


#4

It gives the same error. Both in mine and yours I got the correct result : [3, 5, 7, 9] but the error message says there are two 9s in the list. If I were to reset it, it seems like:

n = [3, 5, 7]
# Add your function here



print list_extender(n)

So n is already something different what is asked for. There must be a problem. How can I sove this?


#7

lets go back to your original code:

n= [1,2,3,4]
# Add your function here
def list_extender(lst):
    n.append(9)
    return n
print list_extender(n)

now, you pass n as argument in the function call:

# function call + argument
print list_extender(n)

so then n is copied into lst (the function parameter), this we can verify:

def list_extender(lst):
    print lst

so then you want to extend lst


#8

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