math exponent函数int属性错误

问题描述 投票:0回答:1

我所看到的一切都表明,从math导入运行指数函数的正确语法是math.exp(some_num.exponent)。当我尝试运行这种格式时,指数给我一个属性错误,说它不是int。我已经定义了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函数协议的回溯:Traceback(最近一次调用最后一次):文件“C:/ Python Projects / Extra Credit / Extra Credit.py”,第47行,c = math.exp(mdic.e)% n AttributeError:'int'对象没有属性'e'

python exponent
1个回答
1
投票
c = math.exp(mdic.e) % n

在这里,您尝试访问eintmdic属性。这引起了错误。

你可能想要:

c = math.pow(mdic, e) % n

同样,让pow为您做模数运算。

c = math.pow(mdic, e, n)

math.exp(x)用于e**x - 其中e是欧拉常数

© www.soinside.com 2019 - 2024. All rights reserved.