2024-08-02: 4. Caesar Cipher

YonnyYonny
1 min read

Here is my Caesar Cipher code. I did this about 8 months ago :)

alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
            'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
            'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']


# user input
# define a function
def cipher(ende, message, shift):
    output = ""
    for char in message:
        if char in alphabet:
            if ende == "encode":
                position = alphabet.index(char) + shift
            else:
                position = alphabet.index(char) - shift
            output += alphabet[position]
        else:
            output += char

    print(f"Here is the {ende}d message: {output}.")


# call defined function
yesno = "yes"
while yesno == "yes":
    ende = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
    message = input("Type your message:\n")
    shift = int(input("Type your shift number:\n"))
    shift = shift % 26

    cipher(ende, message, shift)
    yesno = input("Type 'yes' if you want to go again. Otherwise type 'no'.\n")
    if yesno == "no":
        print("Bye!")
0
Subscribe to my newsletter

Read articles from Yonny directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yonny
Yonny