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.