Share your repls and programming experiences

← Back to all posts
Final Caesar Cipher code
NewStart55 (0)

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']

def encrypt_decrypt(message, enc_shift, enc_direction):
enc_word = ''
if enc_direction == 'decode': enc_shift *= -1
for char in message:
if char in alphabet:
new_location = alphabet.index(char) + enc_shift
if new_location > len(alphabet) - 1: new_location -= len(alphabet)
enc_word += alphabet[new_location]
enc_word += char
print(f"Here's your {enc_direction}'d word: {enc_word} \n")

from art import logo

again = True
while again:
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n").lower()
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
if shift > len(alphabet):
shift = shift - len(alphabet) * (shift // len(alphabet))

encrypt_decrypt(text, shift, direction)

repeat = input('Type "yes" if you want to try again. Otherwise type "no" ').lower()
if repeat != 'yes': again = False