Python Code Challenges: Lists

https://www.codecademy.com/courses/learn-python-3/articles/python-code-challenges-lists

hello i get error from the code under:




lst = [1, 1, 2]


def append_sum(lst) :

    lst = (lst.append(lst[-1] + lst[-2]))

    lst = (lst.append(lst[-1] + lst[-2]))

    lst = (lst.append(lst[-1] + lst[-2]))

    return lst


append_sum(lst)
print(lst)

the error is:

Traceback (most recent call last):
  File "C:/Users/SONY/Desktop/sal's shipping.py", line 13, in <module>
    append_sum(lst)
  File "C:/Users/SONY/Desktop/sal's shipping.py", line 7, in append_sum
    lst = (lst.append(lst[-1] + lst[-2]))
AttributeError: 'NoneType' object has no attribute 'append'

something should be wrong with data type
and when i put list to make the data type right i get anothe error

code:

lst = [1, 1, 2]


def append_sum(lst) :


    lst = list(  lst.append(lst[-1] + lst[-2]))  )

    lst = list(  (lst.append(lst[-1] + lst[-2]))  )

    lst = list(  (lst.append(lst[-1] + lst[-2]))  )
    return lst


append_sum(lst)
print(lst)


error:

 File "C:/Users/SONY/Desktop/sal's shipping.py", line 7
    lst = list(  lst.append(lst[-1] + lst[-2]))  )
                                                 ^
SyntaxError: unmatched ')'

.append() will add to list in memory, but the method itself returns None. (the absence of a return value).

1 Like

thank you , whats the alternate way to return the new list <3

why would you want this? You could of course make a new list with any values you want.

1 Like

thank you a lot
it soved my problem
but is my new code accurate ( im begginer and i havn’t taken loops yet)




lst = [1, 1, 2]


def append_sum(lst) :


    lst.append(lst[-1]+lst[-2])
    lst.append(lst[-1] + lst[-2])
    lst.append(lst[-1] + lst[-2])


    return lst


lst = append_sum(lst)
print(lst)

ideally you want to use loops, given often you don’t know the length of the list. I would recommend coming back to this challenge once you learned loops

1 Like