Python Code Challenges involving Lists -1. Append Sum

The “view solution” for the challenge shows as:

def append_sum(lst):
    lst.append(lst[-1] + lst[-2])
    lst.append(lst[-1] + lst[-2])
    lst.append(lst[-1] + lst[-2])
    return lst

I was thinking it needed to be something where the lst has to be redefined within the function after it is appended, before starting the process over, like this...

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

..but that gives me this....

Traceback (most recent call last):
  File "script.py", line 9, in <module>
    print(append_sum([1, 1, 2]))
  File "script.py", line 4, in append_sum
    lst = lst.append(lst[-1] + lst[-2])
AttributeError: 'NoneType' object has no attribute 'append'

is a list “redefined” after it is appended?

lst.append(lst[-1] + lst[-2]) already modifies lst, so you don’t need a lst =.

I’ll give you a different example

i += 1 # this is modifying i
i = i += 1 #SyntaxError: invalid syntax

@toastedpitabread thank you.

1 Like