Python code challenges - lists

Hi Folks,

In the beginning, apologies if the topic has already been discussed here.
I have a little problem with the 2nd challenge in this set: https://www.codecademy.com/courses/learn-python-3/articles/python-code-challenges-lists

So our goal code is the following:

def append_sum(my_list):
my_list.append(my_list[-1] + my_list[-2])
my_list.append(my_list[-1] + my_list[-2])
my_list.append(my_list[-1] + my_list[-2])
return my_list

When we call the function with a certain list, it adds two last items of the list and adds the sum to my_list. Everything happens trice. Now what I am struggling with is how to evade copypasting the very same line and use a loop.

So my first instinct was to use for and range pattern and came up with something like this
def append_sum(my_list):
for i in range(3):
my_list.append(my_list[-1] + my_list[-2])
return i

The problem is that either there is an intendation bug or it prints me number 2 that doesn’t make sense. I made several combination on for/range, tried while for the sake of trying everything and I remain stuck. Any suggestions?

Sorry for bothering with basics, still learning.

Going forward please format your code in posts.

It’s returning 2 b/c that’s what i is equal to in your list after range executes three times.

The instructions say to return a modified list, not just an element from the list at a particular index.
You can use a for loop with range and len, but you need to change the parameter and the return values.

A good tool to visualize what your code is doing is Python Tutor.
(You have to paste your formatted code into the app though).