-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesar_cipher
More file actions
113 lines (89 loc) · 4.73 KB
/
Copy pathCaesar_cipher
File metadata and controls
113 lines (89 loc) · 4.73 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import caesar_art
print(caesar_art.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']
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"))
def caesar(plain_text,shift_amount,direction_choice): # My Solution
while direction_choice == 'encode' or direction_choice == 'decode':
user_text_letters = []
encode_text = []
decode_text = []
encode_list = []
decode_list = []
direction_choice = direction_choice
plain_text = plain_text
shift_amount = shift_amount
for char in plain_text:
user_text_letters.append(char)
while shift_amount > len(alphabet):
if shift_amount > len(alphabet):
new_shift = shift_amount - len(alphabet)
shift_amount = new_shift
if direction_choice == 'encode':
for i in range(len(user_text_letters)):
for j in range(len(alphabet)):
if user_text_letters[i] == alphabet[j]:
new_index = j + shift_amount
if new_index >= len(alphabet):
new_index = new_index - len(alphabet)
encode_text.append(alphabet[new_index])
for symbol in range(len(user_text_letters)):
if user_text_letters[symbol] not in encode_text and user_text_letters[symbol] not in alphabet and user_text_letters[symbol] != ' ':
encode_text.insert(symbol,user_text_letters[symbol])
elif user_text_letters[symbol] == ' ':
encode_text.insert(symbol, ' ')
elif user_text_letters[symbol] in encode_text and user_text_letters[symbol] not in alphabet and user_text_letters[symbol] != ' ':
encode_text.insert(symbol,user_text_letters[symbol])
for char in encode_text:
encode_list.append(char)
encode_list = ''.join(encode_list)
print(f"The encoded text is {encode_list}")
yes_no = input("Type 'yes' if you want to go again. Otherwise type 'no'. ")
if yes_no == 'yes':
direction_choice = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
plain_text = input("Type your message:\n").lower()
shift_amount = int(input("Type the shift number:\n"))
elif yes_no == 'no':
print("Goodbye.")
break
if direction_choice == 'decode':
user_text_letters = []
encode_text = []
decode_text = []
encode_list = []
decode_list = []
direction_choice = direction_choice
plain_text = plain_text
shift_amount = shift_amount
for char in plain_text:
user_text_letters.append(char)
while shift_amount > len(alphabet):
if shift_amount > len(alphabet):
new_shift = shift_amount - len(alphabet)
shift_amount = new_shift
for x in range(len(user_text_letters)):
for y in range(len(alphabet)):
if user_text_letters[x] == alphabet[y]:
new_index = y - shift_amount
decode_text.append(alphabet[new_index])
for symbol2 in range(len(user_text_letters)):
if user_text_letters[symbol2] not in decode_text and user_text_letters[symbol2] not in alphabet and user_text_letters[symbol2] != ' ':
decode_text.insert(symbol2,user_text_letters[symbol2])
elif user_text_letters[symbol2] == ' ':
decode_text.insert(symbol2, ' ')
elif user_text_letters[symbol2] in decode_text and user_text_letters[symbol2] not in alphabet and user_text_letters[symbol2] != ' ':
decode_text.insert(symbol2,user_text_letters[symbol2])
for char2 in decode_text:
decode_list.append(char2)
decode_list = ''.join(decode_list)
print(f"The decoded text is {decode_list}")
yes_no = input("Type 'yes' if you want to go again. Otherwise type 'no'. ")
if yes_no == 'yes':
direction_choice = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
plain_text = input("Type your message:\n").lower()
shift_amount = int(input("Type the shift number:\n"))
elif yes_no == 'no':
print("Goodbye")
break
caesar(plain_text = text, shift_amount = shift, direction_choice = direction)