Why these code returning "None"?

Hello, I was studying Python Code Cahallenges :Lists

The question is below:

Write a function named append_sum that has one parameter β€” a list named named lst .

The function should add the last two elements of lst together and append the result to lst . It should do this process > three times and then return lst .

For example, if lst started as [1, 1, 2] , the final result should be [1, 1, 2, 3, 5, 8] .

My solution code is below:

def append_sum(lst):
    for i in range(3):
        a = int(lst[-2]) + int(lst[-1])
        b = lst.append(a)
    return b

But this code snippet returns β€œNone”. Why?

Because list.append() works in-place on the list in context and has no return value. We cannot assign it.

    lst.append(a)

return lst
2 Likes

Thank you @mtf for your answer :smiley:

1 Like