【问题标题】:Python caesar cipher how to decode a specific sentence?Python凯撒密码如何解码特定句子?
【发布时间】: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


【解决方案1】:

list.index() 返回index of that item's first occurence in the list。所以表达式plain.index(cipher[(ord(i)-ord('a'))]) 是一个整数,Python 不允许添加到字符串中。

您可能想要的 clean_text 是 plain 中具有 那个索引的元素。所以可以使用标准的list[index] 表示法来获取元素:plain[plain.index(...)]

那行现在是:

clean_text = clean_text + plain[plain.index(cipher[(ord(i)-ord('a'))])]
>>> print(clean_text)
I wxxyzabcdefghhijlmnopqrstuv

其他更改/改进:

  1. 对于if cipher_text.index(i) != " ": 行,表达式永远不会为真,因为list.index() 返回一个整数,如果未找到则返回一个ValueError,它永远不会匹配为等于空格" "。您还已经在循环变量i 中拥有要检查的字符。所以可能是:

    if i != " ":
    
  2. ciphercipher_text 中可能存在拼写错误,字母 x 出现了两次。

  3. 写下你的评论:

    句首还有大写的“I”以及单词之间的空格。 :(

    这来自循环之前的clean_text ='I ' 行。将其更改为 clean_text =''

  4. 不要将字符串与x = x + 'something new' 连接。继续将每个元素附加到列表中,然后使用str.join()

    # before the loop:
    clean_text = []  # empty list
    # inside the loop:
    clean_text.append(plain[plain.index(cipher[(ord(i)-ord('a'))])])
    # later, after the loop, put:
    ''.join(clean_text)
    

【讨论】:

  • 嗨!非常感谢您的友好回答!,我又试了一次,但它仍然说'IndexError:字符串索引超出范围'发生了什么事:(,很抱歉让您为我花费了宝贵的时间。
  • 哦!现在它正在工作,但不能解码:(如果你不介意,我可以再问你一点吗?
  • 我想在评论中说什么,我写了 clean_text = 'I' 因为我不知道如何同时解码小写和大写。如果你不介意,你能提供更多的 cmets 让我的工作正常吗?我开始学习 Python 已经 7 天了 :(,非常感谢您的帮助
  • 这是因为cipherplain 的长度不同(因为cipher 中有两个'x':xx)。所以'y'的索引实际上会给你'z'而'z'你会得到一个错误。 (加入Python聊天室,我有几分钟-chat.stackoverflow.com/rooms/6/python
  • 很遗憾,很遗憾,我不能 :( 上面所说的“您必须在 Stack Overflow 上拥有 20 名声望才能在此处交谈。请参阅常见问题解答。”
猜你喜欢
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
  • 2013-02-26
  • 1970-01-01
  • 2013-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多