import string
import sys
shift = 5
print(' Caesar Cipher '.center(40, '*'))
choices = ['e', 'd']
user_choice = input('Do you wish to [e]ncode, [d]ecode, or quit (any other letter)?: ').lower()
if user_choice not in choices:
print('Program closed.')
sys.exit()
word = input('Enter the word: ')
def encode_words(words, shifts):
"""This encodes a word using Caesar cipher."""
encoded_word = ''
for i in words:
if ord(i) == 32 or ord(i) == 9:
shifted_word = ord(i)
elif i in string.punctuation:
shifted_word = ord(i)
elif i.islower():
shifted_word = ord(i) + shifts
if shifted_word > 122:
shifted_word = (shifted_word - 122) + 96
else:
shifted_word = ord(i) + shifts
if shifted_word > 90:
shifted_word = (shifted_word - 90) + 64
encoded_word = encoded_word + chr(shifted_word)
print('Word:', word)
print('Encoded word:', encoded_word)
def decode_words(words, shifts):
"""This decodes a word using Caesar cipher"""
decoded_word = ''
for i in words:
if ord(i) == 32 or ord(i) == 9:
shifted_word = ord(i)
elif i in string.punctuation:
shifted_word = ord(i)
elif i.islower():
shifted_word = ord(i) - shifts
if shifted_word < 97:
shifted_word = (shifted_word - 97) + 123
else:
shifted_word = ord(i) - shifts
if shifted_word < 65:
shifted_word = (shifted_word - 65) + 91
decoded_word = decoded_word + chr(shifted_word)
print('Word:', word)
print('Decoded word:', decoded_word)
def encode_decode(words, shifts, choice):
"""This checks if the users want to encode or decode, and calls the required function."""
if choice == 'e':
encode_words(words, shifts)
elif choice == 'd':
decode_words(words, shifts)
encode_decode(word, shift, user_choice)