【问题标题】:Decryption function: 'int' object is not callable解密函数:“int”对象不可调用
【发布时间】:2018-02-10 05:08:46
【问题描述】:

我为一个执行单个字符解密的函数编写了代码。我收到此错误消息:'int' object is not callable,但我不知道这是指哪个对象以及我是如何非法调用某些东西的。我做错了什么?谢谢。

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

这是错误回溯消息:

Traceback(最近一次调用最后一次):

文件“”,第 1 行,在 runfile('/Users/brandononeil/Documents/SS18proj04.py', wdir='/Users/brandononeil/Documents')

文件 “/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py”, 第 880 行,在运行文件中 execfile(文件名,命名空间)

文件 “/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py”, 第 102 行,在 execfile 中 exec(编译(f.read(),文件名,'exec'),命名空间)

文件“/Users/brandononeil/Documents/SS18proj04.py”,第 161 行,在 主要()

文件“/Users/brandononeil/Documents/SS18proj04.py”,第 148 行,在 主要的 decr_ch1 = affine_cipher_decryption(ch, rotation, ALPHA_NUM)

文件“/Users/brandononeil/Documents/SS18proj04.py”,第 101 行,在 仿射密码解密 cipher_index = multiplicative_inverse(A,M)(index-N) % M

TypeError: 'int' 对象不可调用

另外,这里是multiplicative_inverse的内容:

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

我已经尝试重命名A_inverse,并且我尝试在affine_cipher_decryption 中写入multiplicative_inverse 函数的内容(这样我就不必调用multiplicative_inverse),但无济于事。

还有什么可能出错的想法吗?

【问题讨论】:

  • 错误回溯消息指出了错误的确切位置。请编辑您的问题以包含此消息。
  • multiplicative_inverse(A,M) 返回什么?
  • multiplicative_inverse(A,M)(index-N) % M 更改为 (multiplicative_inverse(A,M)*(index-N)) % M

标签: python


【解决方案1】:

你的问题是这两行:

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

A_inverse 设置为multiplicative_inverse 的结果,我假设它返回int。第二行尝试调用名为 A_inverse 的函数,但局部变量在作用域中隐藏了该函数。

您可以通过重命名局部变量或函数来修复它。

【讨论】:

  • 我尝试更改命名约定无济于事,我什至尝试在 multiplicative_inverse 的内容中进行编码,因此我不必调用该函数。该程序继续返回相同的错误。我将编辑问题以包括错误回溯消息和相关功能的内容。我仍然不确定出了什么问题?
猜你喜欢
  • 2012-11-04
  • 2015-01-29
  • 2014-03-17
  • 1970-01-01
  • 2022-11-28
  • 1970-01-01
  • 2015-12-19
  • 2017-09-19
  • 2012-04-03
相关资源
最近更新 更多