Day 8 of Python Bootcamp
King Mhar Bayato
Reliability Engineer @ Etihad | AMOS | Data Engineer | SQL | System Analyst
We have been tasked with creating a Caesar cipher program for encoding and decoding. This project presents some challenges and requires meticulous attention to detail and patience through each step. Fortunately, the instructor has provided clear guidance on the specific problems we need to address, which simplifies the process. As a result, we, as students, can concentrate primarily on solving these problems.
You are welcome to download the code for use in your own studies. This allows you to explore and experiment with the concepts at your own pace, enhancing your learning experience.
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']
print("""
,adPPYba, ,adPPYYba, ,adPPYba, ,adPPYba, ,adPPYYba, 8b,dPPYba,
a8" "" "" `Y8 a8P_____88 I8[ "" "" `Y8 88P' "Y8
8b ,adPPPPP88 8PP""""""" `"Y8ba, ,adPPPPP88 88
"8a, ,aa 88, ,88 "8b, ,aa aa ]8I 88, ,88 88
`"Ybbd8"' `"8bbdP"Y8 `"Ybbd8"' `"YbbdP"' `"8bbdP"Y8 88
88 88
"" 88
88
,adPPYba, 88 8b,dPPYba, 88,dPPYba, ,adPPYba, 8b,dPPYba,
a8" "" 88 88P' "8a 88P' "8a a8P_____88 88P' "Y8
8b 88 88 d8 88 88 8PP""""""" 88
"8a, ,aa 88 88b, ,a8" 88 88 "8b, ,aa 88
`"Ybbd8"' 88 88`YbbdP"' 88 88 `"Ybbd8"' 88
88
88
""")
def ceasar(text,shift,cipher_direction):
if cipher_direction.lower() == 'encode':
encoded_text = ''
for letter in text:
if letter in alphabet:
letter_index = alphabet.index(letter)
new_index = (letter_index + shift) % len(alphabet)
encoded_text += alphabet[new_index]
else:
encoded_text += letter
print(f"The encoded text is {encoded_text}")
else:
decoded_text = ''
for letter in text:
if letter in alphabet:
letter_index = alphabet.index(letter)
new_index = (letter_index - shift) % len(alphabet)
decoded_text += alphabet[new_index]
else:
decoded_text += letter
print(f"The encoded text is {decoded_text}")
should_continue = True
while should_continue:
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
ceasar(text,shift,direction)
result = input("Type 'yes' if you want to go again. Otherwise type 'no'.\n")
if result.lower() == 'no':
should_continue = False
elif result.lower() == 'yes':
should_continue = True