Hi I get the following error for my below code for this exercise:

TypeError: ‘int’ object is not iterable

#Write your function here
def append_sum(lst):
first_sum = sum(lst[-1] +lst[-2])
lst.append(first_sum)
second_sum = sum(lst[-1] +lst[-2])
lst.append(second_sum)
third_sum = sum(lst[-1] +lst[-2])
lst.append(third_sum)
return lst
#Uncomment the line below when your function is done
print(append_sum([1, 1, 2]))

Is this because the lst parameter is not updated until the entire function code is finished executing?

Sorry, I don’t see how the syntax is wrong. I have sum(lst[-1] + lst[-2]) that seems to be correct. They’re all the arguments are variables, no strings.

Maybe it’s my english skills the link you provided doesn’t help me with understandind what’s wrong with his syntax. Can you explain it in simple terms? He is not trying to sum strings so I don’t get what’s wrong there

Sums start and the items of an iterable from left to right and returns the total. The iterable ’s items are normally numbers, and the start value is not allowed to be a string.

So, the item within the parentheses must be an iterable (for practical purposes, that means a list or tuple.)

>>> sum(1 + 1)
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
TypeError: 'int' object is not iterable # they are saying, 1 is not an iterable
>>> sum([1,1]) # [1,1] is an iterable (in this case, a list)
2
>>>

That’s not exactly the same no. You can show this by taking one of the versions and replacing the variables to match the others. It won’t match and perhaps then you are able to see the difference.

If you’ve added two numbers together using +, then what do you need sum for? The thing being attempted doesn’t make much sense.

In general you would want to check the documentation of the function you are using to find out how the function behaves in terms of what input it accepts and what output it provides and then you would know how to leverage that function.
Just writing something and hoping the function will deal with it … isn’t so great. That function is going to do whatever it was programmed to do, regardless of what the user means.

But the code he used in this exercise is using int, because list stores only integers, so back to square one, I don’t understand what’s wrong with this:
sum(lst[-1] + lst[-2])

Please mind the fact that this course is my first ever contact with programming so some concepts are strange to me.

You could do that, but if you’re going to type out each term anyway, then you may as well use +

sum is something you’d use if you have a list or other iterable of things to sum precisely so that you don’t have to write out every term.
If you have 1 million terms, or if you don’t know when you’re writing the code how many terms there are going to be… use sum.