DEV Community

Cover image for Caesar Cipher
Scott Gordon
Scott Gordon

Posted on

3 1

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay