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?
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.