coinCount = [2 for i in range(4)]
total = sum(coinCount)
This gives me
TypeError: 'int' object is not callable
I don't understand why because
print type(coinCount)
Gives me
type <'list'>
coinCount = [2 for i in range(4)]
total = sum(coinCount)
This gives me
TypeError: 'int' object is not callable
I don't understand why because
print type(coinCount)
Gives me
type <'list'>
You no doubt used the identifier sum
previously in your code as a local variable name, and the last value you bound to it was an int
. So, in that code snippet, you're trying to call the int
. print sum
just before you try calling it and you'll see, but code inspection will probably reveal it faster.
This kind of problem is why expert Pythonistas keep telling newbies over and over "don't use builtin names for your own variables!" even when it's apparently not hurting a specific code snippet: it's a horrible habit, and a bug just waiting to happen, if you use identifiers such as sum
, file
, list
, etc, as your own variables or functions!-)
I'd like to add on to what Alex Martelli said by saying don't use sum as a variable name but if you're program is too long already or you're using idle and you don't want to start over you can do
sum2 = sum
del sum
to transfer the value of sum into sum2 (if necessary) and delete the variable sum (allowing you to continue working with the sum function)