Python Code Challenges (Lists) - Append Sum


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:

  1. Define the function to accept one parameter for our list of numbers
  2. Add the last and second to last elements from our list together
  3. Append the calculated value to the end of our list.
  4. Repeat steps 2 and 3 two more times
  5. Return the modified list
#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]))

Remember to manually increment counter.

1 Like