【问题标题】:Python shift with alphabet带有字母表的 Python 移位
【发布时间】:2016-02-03 13:16:23
【问题描述】:
def menu():
    choice = input("Press 1 to encode, 2 to decode, 9 to exit ")
    return choice

def makeKeycode(message):
    key = input("What is the key? ").upper()
    length = len(message)
    keycode = ""
    counter = 0
    while length >0:
        if counter == len(key):
            counter = 0
        keycode = keycode + key[counter]
        counter = counter + 1
        length = length - 1
    print(keycode)
    return keycode

def enterMessage():
    message = input("What is the message ").upper()
    return message

def encodeMessage(message, keycode):
    ciphertext =""
    alphabet= "ABCDEFGHIJKLMNOPQRTUVWXYZ"
    for i in range (len(message)):
        character = message[i]
        charCode = alphabet.find(character)
        keycodeChar =keycode[i]
        keyLetter = alphabet.find(keycodeChar)
        position = (charCode + keyLetter)%25
        cipherLetter = alphabet[position]
        ciphertext =ciphertext + cipherLetter
    return ciphertext

def decodeMessage(ciphertext,keycode):
    ciphertext =""
    alphabet= "ABCDEFGHIJKLMNOPQRTUVWXYZ"
    for i in range (len(ciphertext)):
        character = ciphertext[i]
        charCode = alphabet.find(character)
        keycodeChar =keycode[i]
        keyLetter = alphabet.find(keycodeChar)
        position = (charCode - keyLetter)%25
        cipherLetter = alphabet[position]
        ciphertext =ciphertext - cipherLetter
    return message

def enterCipher ():
    ciphertext = input("Enter the text to be decoded")
    return ciphertext

def encode():
    message = enterMessage()
    keycode = makeKeycode(message)
    ciphertext = encodeMessage(message,keycode)
    print(ciphertext)
def decode():

    keycode = makeKeycode(ciphertext)
    message = decodeMessage(ciphertext, keycode)
    print (message)   

def main():
    MyDictionary=("A:1","B:2","C:3","D:4","E:5","F:6","G:7","H:8","I:9","J:10","K:11","L:12","M:13","N:14","O:15","P:16","Q:17","R:18","S:19","T:20","U:21","V:22","W:23","X:24","Y:25","X:26")
    print (MyDictionary)
    choice = 0
    while choice !=9:
        choice = int(menu())

        if choice == 1:
            encode()
        elif choice == 2:
            decode()

if __name__ == "__main__":
    main()

嗨,我无法让我的代码工作,我的编码功能可以工作,但我正在努力修复我的解码功能。我不明白我哪里出错了。我希望能够解码我将编码的消息,但这不起作用,因为解码功能会停止程序。我在编码中犯了错误吗?感谢所有的帮助 问候

【问题讨论】:

  • 好吧,这里有个问题,如果你的编码信息是“11”,那是“K”还是“AA”?
  • 当您说您的解码消息停止程序时,您遇到了什么错误?究竟是什么问题?
  • 请不要破坏您的帖子。请注意,一旦您在本网站上发布了问题或答案,这些帖子就会成为也为该内容做出贡献的其他人的集体努力的一部分。除非在特殊情况下,否则不应删除可能对其他人有用的帖子。即使该帖子对原作者不再有用,该信息仍然对将来可能遇到类似问题的其他人有益 - 这是 Stack Exchange 的基本理念。

标签: python decode shift


【解决方案1】:
def decodeMessage(ciphertext,keycode):
    ciphertext =""

您似乎正在将密文重置为空字符串。结果,您的 len(ciphertext) 为 0,这意味着您什么也没做。

python 2.75

sig = 'abcd'
def test(sig):
    sig = ""
    print 'sig = ', sig

>>> test(sig)
    sig = ''
>>> print sig
    'abcd'

【讨论】:

    【解决方案2】:

    我发现您的解码消息功能存在两个问题:

    def decodeMessage(ciphertext,keycode):
        ciphertext =""
        alphabet= "ABCDEFGHIJKLMNOPQRTUVWXYZ"
        for i in range (len(ciphertext)):
            character = ciphertext[i]
            charCode = alphabet.find(character)
            keycodeChar =keycode[i]
            keyLetter = alphabet.find(keycodeChar)
            position = (charCode - keyLetter)%25
            cipherLetter = alphabet[position]
            ciphertext =ciphertext - cipherLetter
        return message
    

    我发现的第一个问题是您将 ciphertext 作为强制参数传递给 decodeMessage,然后将 ciphertext 的值更改为“”(一个空字符串)。我建议将参数中的密文更改为“ct”之类的内容,以防止重叠。

    我发现的第二个问题是您返回message,但您没有对消息执行任何操作。运行此程序时,您将收到一个全局错误,因为 message 不是全局的,但您在本地范围内调用 message 而没有在本地定义它。

    现在,谈另一件事:您的 encode 方法没有正确编码任何内容。看下面的输出:

    Press 1 to encode, 2 to decode, 9 to exit 1
    What is the message some
    What is the key? 18
    YNLD
    Press 1 to encode, 2 to decode, 9 to exit 1
    What is the message some
    What is the key? 5
    YNLD
    Press 1 to encode, 2 to decode, 9 to exit 1
    What is the message some
    What is the key? 1
    YNLD
    

    无论密钥如何,所有“一些”都被编码为“YNLD”。要使用 Caesar Cypher 加密某些内容,您将需要使用 ords 和 chrs。 Ord 将字符变为数字,Chr 将数字变为字母。以 Python 3 中的以下函数为例:

    from string import ascii_letters
    def cypher_letter(letter, key):
        if letter not in ascii_letters: #commas, question marks, etc
            return letter 
        if letter.islower(): #lowercase and uppercase have differing ord values
            lower, upper = ord('a'), ord('z') # values: 97 to 122 
        elif letter.isupper():
            lower, upper = ord('A'), ord('Z') # values: 65 to 90
        else:
            print("Something went wrong.") #probably didn't enter valid letters
    
        cypher = ord(letter) + key #if key is "1" and letter is 'a', 97 + 1
        if cypher < lower: cypher += 26 #26 letters in alphabet, this is for looping
        if cypher > upper: cypher -= 26 #back around if it exceeds upper,lower limits
        return chr(cypher) #chr(98) = 'b'
    

    以下代码:

    cyphered_words = []
    keys = [18, 5, 1]
    for key in keys:
        encoded = ""
        for letter in 'some':
            encoded += (cypher_letter(letter, key)) #to cypher we pass the key
        print(encoded)
        cyphered_word.append(encoded)
    

    输出这个:

    Kgewlzafy hjwllq dgfy, oalz ugehdwp dwllwjk!
    Xtrjymnsl uwjyyd qtsl, bnym htruqjc qjyyjwx!
    Tpnfuijoh qsfuuz mpoh, xjui dpnqmfy mfuufst!
    

    密码和解密几乎是一回事。就像它们是阴阳之类的,所以解密函数看起来像:

    def decrypt_message(message, key):
        decyphered_text = ""
        for letter in message:
            decyphered_text += cypher_letter(letter, (key*-1)) #to decypher we pass the negative key 
        return decyphered_text
    

    还有一个循环来解密我们的 3 'some's:

    for count, word in enumerate(cyphered_words):
        print(decrypt_message_k(word, keys[count]))
    

    还有输出:

    Something pretty long, with complex letters!
    Something pretty long, with complex letters!
    Something pretty long, with complex letters!
    

    我希望这可以帮助您走上正轨。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多