Caesar Cipher issue

后端 未结 1 1635
温柔的废话
温柔的废话 2020-12-02 01:27

I am trying to implement a Caesar cipher.

I have tried to return message in the function, but I get an error message (outside function). Can anyone help, please?

1条回答
  •  离开以前
    2020-12-02 02:12

    Here's Python 3 Caesar cipher implementation that uses str.translate():

    #!/usr/bin/env python3
    import string
    
    def caesar(plaintext, shift, alphabet=string.ascii_lowercase):
        shifted_alphabet = alphabet[shift:] + alphabet[:shift]
        return plaintext.translate(plaintext.maketrans(alphabet, shifted_alphabet))
    
    message = input("type message to encode")
    shift = int(input("Enter number to code "))
    print(caesar(message.lower(), shift))
    

    Here's Python 2 version of Caesar Cipher.

    0 讨论(0)
提交回复
热议问题