2023-12-31
Caesar Cipher in Python:
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']
#import logo from art module
from art import logo
print(logo)
#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
yesorno = "yes"
while yesorno == "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)
yesorno = input("Type 'yes' if you want to go again. Otherwise type 'no'.\n")
if yesorno == "no":
print("Bye!")