I have an understanding on how to solve this problem, both by using an append function three times in succession, as well as with the range() option. However, could somoene help with understand why my While Loop option isn’t working? Thanks!
Here are the instructions and code:
Let’s create a function that calculates the sum of the last two elements of a list and appends it to the end. After doing so, it will repeat this process two more times and return the resulting list. You can choose to use a loop or manually use three lines. Here are the steps we need:
- Define the function to accept one parameter for our list of numbers
- Add the last and second to last elements from our list together
- Append the calculated value to the end of our list.
- Repeat steps 2 and 3 two more times
- Return the modified list
#CORRECT Answer #def append_sum(lst): #lst.append(lst[-1] + lst[-2]) #lst.append(lst[-1] + lst[-2]) #lst.append(lst[-1] + lst[-2]) #return lst #Loop Attempt def append_sum(lst): counter = 0 while counter < 3: lst.append(lst[-1] + lst[-2) return lst #Uncomment the line below when your function is done print(append_sum([1, 1, 2]))