# How to use the sum() function?

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?

That’s not the right syntax for sum().

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.

Is it that sum can’t work on index values?

No, it’s not correct.
Wrong:

``````sum(1 + 1)
``````

Right:
?
(Look it up: link in my prior post)

A little silliness, but a working example…

``````array = [1, 1, 2]
print (sum(array[-1] + array[-2]))

Traceback (most recent call last):
File "main.py", line 19, in <module>
print (sum(array[-1] + array[-2]))
TypeError: 'int' object is not iterable
``````
``````print (sum([array[-1], array[-2]]))
# 3
``````
1 Like

This is how I solved this lesson. Hope it helps out!

``````
def append_sum(lst):
two_lsts = lst[-1] + lst[-2]
lst.append(two_lsts)
full_list = list(lst)
return full_list

``````

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

You don’t need that last step, since you’re appendind lst every time you can just type “return lst”, it worked for me.

Alright, thank you for the information!

`sum` ( iterable , / , start=0 )

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
>>>
``````

And he is doing exactly that:

`sum(lst[-1] + lst[-2])`

What is wrong with that?

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.

So lst[-1 is not an iterable?

what it is depends on how you defined it
but it isn’t what you’re passing to sum anyway

If you do this:

``````print(1 + 1)
``````

Then you are not telling print about 1, 1, and (+), you are telling print about 2

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.

If you use +, why do you need sum?

Hey let me add these two numbers together, and then get the sum of that. <-- you’re saying it twice

Ok, now I finally get it, he should have just used comma Thanks you this explanation was simple enough for me to get it

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.

If you’re going to add two numbers, use +

1 Like