数学指数函数int属性错误

时间:2019-05-01 15:16:57

标签: python exponent

我所看到的一切都表明,从数学导入中运行指数函数的正确语法是math.exp(some_num.exponent)。当我尝试运行这种格式时,指数给了我一个属性错误,说它不是整数。 我已经定义了e = 29,并且正在尝试使用e作为指数。 另外n = 16637

我也尝试过仅使用**运算符。这使我可以使用2变量(mdic,e)进行计算,但是当它运行程序时,我会遇到溢出错误。

p = 127
q = 131
n = p * q
thetan = (p-1)*(q-1)
e = 29
if e < thetan:
    if math.gcd(e, thetan) == 1 and (e > 25):
        print("e = ", e)
print("gcd of ", e, " and thetan == ", math.gcd(e, thetan))
print("Public keys == ", e, ",", n)
for k in range(1,10):
    d = (k * thetan + 1)/ e
    if d / 1 == d // 1:
        print("d (private key) == ", d)
        print("k == ", k)
k = 6
d = (k * thetan + 1)/ e
print("e == ", e)
print("e*d -1 = ", (e*d -1))
m = {   50:'What is up?',
        51:'You are fast!',
        52:'All your trinkets belong to us.',
        53:'Someone on our team thinks someone on your team are in the same class.',
        54:'You are the weakest link.',
        55:'Encryption is fun;',
        56:'Spring is my favorite season',
        57:'Enjoy your morning beverage',
        58:'I am an early riser',
        59:'I am not an early riser',
        60:'Wake Tech is my school',
        61:'CSC 120 Computing Fundamentals',
        62:'Best wishes to you'

mdic = int(input("Enter an integer from the dictionary 'm' = {50 - 62}: "))
while mdic not in m:
    print("Input not a valid integer within the m dictionary.")
    mdic = int(input("Enter an integer from the dictionary 'm': "))

c = math.pow(mdic, e) % n
print("The encrypted text is: ", c)
m = math.pow(c, d) % n
print(m)

感谢您的指导。 使用math.exp函数协议的回溯: 追溯(最近一次通话):   在第47行的文件“ C:/ Python Projects / Extra Credit / Extra Credit.py”中     c = math.exp(mdic.e)%n AttributeError:“ int”对象没有属性“ e”

1 个答案:

答案 0 :(得分:1)

c = math.exp(mdic.e) % n

在这里,您尝试访问eint的{​​{1}}属性。这引发了错误。

您可能想要:

mdic

等效地,让c = math.pow(mdic, e) % n 为您做模运算。

pow

c = math.pow(mdic, e, n) 用于math.exp(x)-其中e**x是欧拉常数