DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,673 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
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)

Take a look at this:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›