-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
57 lines (45 loc) · 1.96 KB
/
main.py
File metadata and controls
57 lines (45 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# The Caeser Cipher
# encode or decode your text using your own secret key (shift_amount)
# importing required libraries
from os import path
from art import logo
import time
# creating a alphabet array to shift letters from and to new positions inside user_text
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']
# while loop condition
Quit = False
def CaesarCipher(plain_text, shift_amount, cipher_direction):
""" a function to do encoding and decoding """
end_chipher = "" # empty string
end_text = "encoded"
# making shift_amount negative if direction is decode
if cipher_direction == 'decode':
shift_amount *= -1
end_text = "decoded"
# main loop
for letter in plain_text:
if letter not in alphabet:
# if a number, symbol or a space leave it as it is
end_chipher += letter
else:
position = alphabet.index(letter)
new_position = position + shift_amount
end_chipher += alphabet[new_position]
return print(f"\nThe {end_text} text is:\n{end_chipher}\n")
while not Quit:
# welcome msg and logo
print("\n\nWelcome to Caeser Cipher created by YJ-928\n", logo)
# taking inputs
direction = input(
"\nType 'encode' to encrypt, type 'decode' to decrypt:\n").lower()
text = input("\nType your message:\n").lower()
shift = int(input("\nType the shift number:\n"))
# if the user enters a shift number greater than 26.
shift = shift % 26
# calling function
CaesarCipher(plain_text=text, shift_amount=shift,
cipher_direction=direction)
# end loop condition, if user wants to quit
if input("\ndo you want to encode/decode again? type Y or N: ").upper() == "N":
Quit = True