List manipulation in functions


#1



[I dont see why this is not accepted](https://www.codecademy.com/courses/python-beginner-nzzVa/2/4?curriculum_id=4f89dab3d788890003000096) 




here my code:

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

print list_extender(n)

Here the error:   
Oops, try again. list_extender([1, 2, 3, 4]) returned [3, 5, 7, 9] instead of [1, 2, 3, 4, 9]

I think the error lies in the return of the list, but ive not been told that the list will be modified for testing otherwise im pretty sure my code would be fine?


#2

Your function should return a value based on the argument given to it.

However, your function's return value is not at all affected by the content of the list that is given to it as an argument, you lost track of that list, you return another one.

This is a bit like having a function that adds two numbers, but only ever returns 157, regardless of what the two numbers are.


#3

so would i return lst? sorry im a noob at this


#4

ahhh i added n = lst in my function so that n can be changeable and that worked. thanks for giving me a different perspective :slight_smile:


#5

Your function should only be concerned with the list that is given to it as an argument

n is an entirely different variable, the function shouldn't be mentioning it at all

Also, if you create a variable named n in your function, then that's a different variable from your global n - what you would have done is to have two variables refer to the same value which is fine and all but seems like it's dead code that can be removed


#6

You want to return lst not n


#7

i did try that but it didn't work, so i added

n = lst in my function and it worked.

would this be considered sloppy programming? i thought i was quite clever doing that, but maybe im just making my code unnecessarily long?


#8

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