DEV Community

loading...
Cover image for My #100daysOfCode Challenge - Python 100 projects in 100 days - Journal Entries - Day 8

My #100daysOfCode Challenge - Python 100 projects in 100 days - Journal Entries - Day 8

abeck617 profile image Anthony Beckford🚀 ・2 min read

Day 8 - Caesar Cipher

For this project I built a Caesar Cipher from learning about Functions with inputs, Difference between Arguments and Parameters

logo = """

,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 88YbbdP"' 88 88 `"Ybbd8"' 88

88

88

"""

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

def caesar(start_text, shift_amount, cipher_direction):
end_text = ""
if cipher_direction == "decode":
shift_amount *= -1
for char in start_text:
if char in alphabet:
position = alphabet.index(char)
new_position = position + shift_amount
end_text += alphabet[new_position]
else:
end_text += char

#TODO-3: What happens if the user enters a number/symbol/space?
#Can you fix the code to keep the number/symbol/space when the text is encoded/decoded?
#e.g. start_text = "meet me at 3"
#end_text = "•••• •• •• 3"
Enter fullscreen mode Exit fullscreen mode

print(f"Here's the {cipher_direction}d result: {end_text}")

TODO-1: Import and print the logo from art.py when the program starts.

from art import logo

TODO-4: Can you figure out a way to ask the user if they want to restart the cipher program?

e.g. Type 'yes' if you want to go again. Otherwise type 'no'.

If they type 'yes' then ask them for the direction/text/shift again and call the caesar() function again?

Hint: Try creating a new function that calls itself if they type 'yes'.

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"))

#TODO-2: What if the user enters a shift that is greater than the number of letters in the alphabet?
#Try running the program and entering a shift number of 45.
#Hint: Think about how you can use the modulus (%).

shift = shift % 25

caesar(start_text=text, shift_amount=shift, cipher_direction=direction)

input("Type 'yes' if you want to go again. Otherwise type 'no'.\n")
if results == 'no':
should_continue = False
print("Goodbye")

Discussion (0)

pic
Editor guide