DEV Community

Cover image for Caesar Cipher
Scott Gordon
Scott Gordon

Posted on

Caesar Cipher

# exercise7.py
# This program can encode and decode Caesar ciphers.
# by: Scott Gordon

def main():
    # Create a welcome message.
    print("***** Welcome to the Ceasar Cipher Encoder / Decoder ******")

    encode_decode_choice = int(
        input("Enter 1 to encode or enter 2 to decode: "))
    result = ""

    if encode_decode_choice == 1:
        # Input a string of plaintext to encode, and the value of a key.
        text = input("Enter the text you would like to encode here: ")
        key = int(input("Enter the Caesar cipher key here: "))
        # TODO Shift the letters of the string pus the number of the key.
        for ch in text:
            result += chr((ord(ch))+key)

    elif encode_decode_choice == 2:
        # Input a string of plaintext to decode, and the value of a key.
        text = input("Enter the text you would like to decode here: ")
        key = int(input("Enter the Caesar cipher key here: "))
        # Shift the letters of the string minus the number of the key.
        for ch in text:
            result += chr((ord(ch))-key)

    # Output the encoded / decoded message 
    print(result)


main()

Enter fullscreen mode Exit fullscreen mode

Photo by Ilona Frey on Unsplash

Top comments (0)