问题
After reading a few errors of the type 'int not callable' on stackoverflow, I see that most errors of the type involve treating an int like a function. I am getting this error on the following program and I'm not sure what's going on:
find the power of n that satisfies the equation
for n in range(100):
if ((2^n // 3) % 2) == 1:
print n
The error traceback reads:
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
回答1:
You have a variable named range
, which you're assigning an integer to. So when you do
for n in range(100):
it's trying to call the integer as a function, instead of using the built-in range
function.
The best solution is not to reuse builtin functions as variable names. But if you really want to, you can still access the original function using the __builtin__
module.
import __builtin__
for n in __builtin__.range(100):
回答2:
^ is python is Bitwise Exclusive Or
** is the operator that you're looking for
for n in range(100):
if ((2**n // 3) % 2) == 1:
print n
as for the erorr that you're getting,
int not callable
It's not reproducible, it's probably not these lines that's causing it
来源:https://stackoverflow.com/questions/32489016/modulo-computation-in-python-int-not-callable