【发布时间】:2015-11-23 11:43:16
【问题描述】:
我的加密工作正常,但是当我运行解密代码时,它不起作用。当我到达那部分代码时,会出现错误 -
cipher2 += cipher[(cipher(A)-key1)%(len(cipher2))] TypeError:“str”对象不可调用如果您能花时间帮助我,我将不胜感激。
alphabetL = 'abcdefghijklmnopqrstuvwxyz'
alphabetC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
number = '0123456789'
space = ' '
Ll = len(alphabetL)
Lc = len(alphabetC)
Ln = len(number)
Lall = Ll + Lc + Ln
msgall = alphabetL + alphabetC + number + space
Question1 = input("Hello, please insert the message you want encrypted: ")
key1 = int(input("Please insert the key you want used [Keep between 1 and 26]: "))
cipher = ''
cipher2 = ''
for A in Question1:
if A in alphabetL:
cipher += alphabetL[(alphabetL.index(A)+key1)%Ll]
elif A in alphabetC:
cipher += alphabetC[(alphabetC.index(A)+key1)%Lc]
elif A in number:
cipher += number[(number.index(A)+key1)%Ln]
elif A in space:
cipher += space
else:
print ("Error, please use abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
print (cipher)
Question2 = input("Would you like to decrypt the message? [Y/N]: ")
if Question2 == "Y":
for A in cipher:
cipher2 += cipher[(cipher(A)-key1)%(len(cipher2))]
print (cipher2)
【问题讨论】:
-
问题出在倒数第二行的
cipher(A)部分。cipher变量是一个字符串,不能像函数一样调用。您是否尝试改为调用.index()方法? -
我不确定。你会把它改成什么?
-
您可能需要考虑使用
enumerate()而不是在字符串本身上调用.index()方法。 -
对不起,没用
-
在倒数第二行调用
cipher(A)的目的是什么?
标签: python encryption caesar-cipher