Brandon O'Neil
Brandon O'Neil

Reputation: 103

Decryption function: 'int' object is not callable

I've written code for a function that executes decryption of individual characters. I received this error message: 'int' object is not callable, but I don't know which object this is referring to and how exactly I'm illegally calling something. What did I do wrong? Thanks.

def affine_cipher_decryption(ch,N,alphabet):
    M = len(alphabet)
    A = get_smallest_co_prime(M)
    A_inverse = multiplicative_inverse(A,M)
    counter = -1
    for element in alphabet:
        if element == ch:
            counter += 1
            index = counter
            break
        else:
            counter += 1
    cipher_index = A_inverse(index-N) % M
    cipher_ch = alphabet[cipher_index]
    return cipher_ch

Here is the error traceback message:

Traceback (most recent call last):

File "", line 1, in runfile('/Users/brandononeil/Documents/SS18proj04.py', wdir='/Users/brandononeil/Documents')

File "/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile execfile(filename, namespace)

File "/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "/Users/brandononeil/Documents/SS18proj04.py", line 161, in main()

File "/Users/brandononeil/Documents/SS18proj04.py", line 148, in main decr_ch1 = affine_cipher_decryption(ch, rotation, ALPHA_NUM)

File "/Users/brandononeil/Documents/SS18proj04.py", line 101, in affine_cipher_decryption cipher_index = multiplicative_inverse(A,M)(index-N) % M

TypeError: 'int' object is not callable

Also, here are the contents of multiplicative_inverse:

def multiplicative_inverse(A,M):
    for x in range(M):
        if (A*x)%M == 1:
            return x

I've already tried renaming A_inverse, and I've tried writing the contents of the multiplicative_inverse function inside of affine_cipher_decryption (so I wouldn't have to call multiplicative_inverse) to no avail.

Any ideas of what else could be wrong?

Upvotes: 0

Views: 95

Answers (1)

Albert
Albert

Reputation: 337

Your problem is these two lines:

A_inverse = multiplicative_inverse(A,M)
cipher_index = A_inverse(index-N) % M

A_inverse is set to the result of multiplicative_inverse which I'm assuming returns an int. The second line tries to call a function named A_inverse but the local variable shadows the function in the scope.

You can fix it by renaming either the local variable or the function.

Upvotes: 2

Related Questions