Hello,
I have worked my way through the topic Lists in Python, but have had to stop during the first code challenge. The challenge reads as follows:
Write a function named
append_sum
that has one parameter — a list named namedlst
.The function should add the last two elements oflst
together and append the result tolst
. It should do this process three times and then returnlst
.
For example, iflst
started as[1, 1, 2]
, the final result should be[1, 1, 2, 3, 5, 8]
.
I believe one of the key concepts is to break down the actual task, so I have started to create this function only aiming to add one element to the list.:
def append_sum(lst):
sum_elements = (lst[-2] + lst[-1])
return lst.append(sum_elements)
However, the function evaluates to None
, once an input is given. I guess, my question is, why is that ?
I was under the impression, that it should work like this:
lst = [1, 2, 3]
lst.append(4)
print(lst)
[1, 2, 3, 4]
It is my first question posted. Please let me know, if I did not follow any community guidelines.
Thanks, Michael.