-
Notifications
You must be signed in to change notification settings - Fork 0
/
ceasar_cipher.py
56 lines (37 loc) · 1.81 KB
/
ceasar_cipher.py
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
from art import logo
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 ceasar(new_text, new_shift, new_direction):
end_text = ""
for letter in new_text:
position = alphabet.index(letter)
if(new_direction == "encode"):
new_position = position + new_shift
if(new_position >= len(alphabet)):
position = alphabet.index("a")
new_position = (position - 1) + new_shift
end_text += alphabet[new_position]
elif(new_direction == "decode"):
new_position = position - new_shift
if(new_position >= len(alphabet)):
position = alphabet.index("a")
new_position = (position - 1) - new_shift
end_text += alphabet[new_position]
print(f"The {new_direction} text is {end_text}")
def run():
access = True
while access:
try:
print(logo)
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt: type 'exit' to out of app:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
shift = shift % 26
ceasar(new_text=text, new_shift=shift, new_direction= direction)
restart = input("Type 'yes' if you want to go again. Otherwise type 'no'.\n")
if (restart == "no"):
access = False
print("Goodbye")
except ValueError:
print("Invalid value")
if __name__== '__main__':
run()