How do I make this in to a loop?

Hi,

I am doing python code challenges, and I am on lists.
I want to make a loop using “range()”, but I don`t know how to write it. Can anyone help?

The code:

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

The solution also has “1st” written instead of “first”, but I don`t know how that is possible, can anyone explain it to me?

You can do a for-loop that iterates from 0 to 2 (meaning 3 iterations) if you use range(3)

def append_sum(first):
  for (i in range(3)):
    first.append(first[-1] + first[-2])
return first
2 Likes