我对以下代码有疑问:
ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def main():
encrypt_code = codeer('NOBODY EXPECTS THE SPANISH INQUISITION!', 'CIRCUS')
print("{}".format(encrypt_code))
print("{}".format(decrypt('CIRCUS', encrypt_code)))
def codeer(tekst, sleutel):
pairs = zip(tekst, cycle(sleutel))
code = ""
for pair in pairs:
total = reduce(lambda x, y: ALPHA.index(x) + ALPHA.index(y), pair)
code += ALPHA[total % 26]
return code;
此代码将崩溃,因为该消息包含一个空格和一个!标志。
total = reduce(lambda x,y:ALPHA.index(x)+ ALPHA.index(y),pair) ValueError:找不到子字符串
任何人都可以帮我解决我的问题
以下是预期输出的示例:
codeer('NOBODY EXPECTS THE SPANISH INQUISITION!', 'CIRCUS')
'PWSQXQ MORYUVA VBW AGCHAUP KHIWQJKNAQV!'
decodeer('PWSQXQ MORYUVA VBW AGCHAUP KHIWQJKNAQV!', 'CIRCUS')
'NOBODY EXPECTS THE SPANISH INQUISITION!'
答案 0 :(得分:0)