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] .
I’m not sure where exactly you’re getting stuck. To solve this first challenge, consider how you can access the last element of a list, and also the second to last element. You’ll want to add the values of those elements together, and then using something like append() add the resulting value to the end of the list. Hopefully this will help you to get started.
Try to explain better where you get stuck or what you do not understand how to do… anyway for access to the last 2 value of the list you should index them and then append everything to the list… and repeat this 3 time .
Good job! Your code gets the job done but you can use a slice to add the last two elements of the list together and use a for loop to repeat the process 3 times:
def append_sum(lst):
# for-loop to repeat the following process 3x
for i in range(3):
# append the sum of the last two elements in list
lst.append(sum(lst[-2:]))
return lst