modulo computation in python - int not callable?

守給你的承諾、 提交于 2019-12-12 00:55:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!