sds2019 icon indicating copy to clipboard operation
sds2019 copied to clipboard

sum([1,2,3]) error: TypeError: 'int' object is not callable

Open IAmAndreasSK opened this issue 5 years ago • 4 comments

Hi,

I have been playing around with Python in Jupyter Notebook (not necessarily directly relevant for the assignment). I tried to run the sum() function on a list with a start value of 10:

thirdlist=[1,2,3,4,5] x = sum(thirdlist,10) print(x)

I get the following output:

`--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () 1 thirdlist=[1,2,3,4,5] ----> 2 x = sum(thirdlist,10) 3 print(x)

TypeError: 'int' object is not callable`

I can't seem to understand what I'm doing wrong? I've tried Googling without any look. Moreover, when I try the same code in SublimeText and PythonTutor's Visualizer, it seems to give the correct output (25).

Thank you, Andreas

IAmAndreasSK avatar Jul 22 '19 09:07 IAmAndreasSK

Hi Andreas,

What does the rest of your notebook look like? Did you "overwrite" the sum() function by assigning an integer value to sum somewhere in the script? That might explain why it works fine when run on its own elsewhere.

mathiasbruun avatar Jul 22 '19 10:07 mathiasbruun

As Mathias pointed out, you have most likely overwritten the variable sum somewhere earlier in your code. I.e. your code contains something like

sum = 2+3 
sum([1,2,3]) # Fails

What you are doing is then something like 5([1,2,3]) and since 5 is not callable you get an error.

kristianolesenlarsen avatar Jul 22 '19 12:07 kristianolesenlarsen

Hey Kristian and Mathias, thanks! I did 'define' it in several places. I have renamed them so they're all unique and it works smoothly now :)

Thanks!

IAmAndreasSK avatar Jul 22 '19 17:07 IAmAndreasSK

Great! I'm reopening the issue to make it easier for others to find.

kristianolesenlarsen avatar Jul 23 '19 07:07 kristianolesenlarsen