【发布时间】:2020-04-03 01:47:53
【问题描述】:
我正在尝试解码加密消息'lmnopqrstuvwxyzabcdefghijk'。我知道它移动了 11,我必须破译 'I wtvp olel decfnefcpd lyo lwrzctesxd'。
这是我到目前为止写的内容:
#enciphered message = 'I wtvp olel decfnefcpd lyo lwrzctenter code hereesxd'
plain = 'abcdefghijklmnopqrstuvwxyz'
#plain Index= 0123456789........25
cipher = 'lmnopqrstuvwxxyzabcdefghijk'
#11 12 13 14 15 16 17 18 19 20 21 22 23 24 25...12345678910
cipher_text = input('Enter enciphered message: ')
clean_text ='I '
for i in cipher_text:
if cipher_text.index(i) != " ":
clean_text = clean_text + plain.index(cipher[(ord(i)-ord('a'))])
else:
clean_text = clean_text + " "
print(clean_text)
当我运行它时:
Enter enciphered message: I wtvp olel decfnefcpd lyo lwrzctesxd
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-97-ac338a9d79fc> in <module>
18 for i in cipher_text:
19 if cipher_text.index(i) != " ":
---> 20 clean_text = clean_text + plain.index(cipher[(ord(i)-ord('a'))])
21 #chr((cipher.index(cipher_text[i]))+ ord('a')) - 11)
22 else:
TypeError: can only concatenate str (not "int") to str
我对 Python 很陌生,不知道如何解决。
添加推荐:我要解码的加密消息有大写“I”以及单词之间的空格,所以我想知道如何同时解码大写和小写
【问题讨论】:
-
这能回答你的问题吗? Caesar Cipher issue
-
@colidyre 不,:(,我是 Python 的新手,所以我确实想了解它,我对 def 的东西一无所知。老实说,我不知道如何解码它,它不工作。如果你有任何机会和时间,你能看看我制作的代码,并给我一些评论让它工作吗?:(谢谢。
-
@colidyre 它在句子的开头还有大写的“I”以及单词之间的空格。 :(
标签: python indexing caesar-cipher chr