Python Code Challenges: Lists

Python Code Challenges: Lists first
1.Append Sum

Task is simple:
Take list, add two last element together and then append to the same exact list three times.

I wrote this piece of code

def append_sum(lst):
  for x in range(3):
    lst.append(lst[-1] + lst[-2])

append_sum([1, 1, 2])

#[1, 1, 2, 3, 5, 8]

According to the task this is correct answer. But after checking it system says it is incorrect.
I`ve load this snippet in VSCode and its gave me the same result.
Help button gave me this:

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

print(append_sum([1, 1, 2]))

Which is output the same result. Well, my solution is better but it is incorrect for some reason. I am a bit confused by that.

Read the prompt very carefully. There is one crucial thing your function is missing.

1 Like

It doesn’t

Return lst

I was trying to get right answer, not follow exact orders. If that`s the case then i thank you, that was very stupid of me.

It wasn’t stupid of you, it’s just why your answer wasn’t accepted by Codecademy. It’s also a good reminder to be aware of whether your function returns something or not. It’s very easy to assume you put a return statement when you see the correct output printed to your terminal.

Happy coding!


How well do you understand indexing in Python?

1 Like