David J.
David J.

Reputation: 1913

modulo computation in python - int not callable?

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

Upvotes: 0

Views: 101

Answers (2)

Barmar
Barmar

Reputation: 781255

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):

Upvotes: 1

taesu
taesu

Reputation: 4580

^ 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

Upvotes: 0

Related Questions