-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8_caesar_cipher.py
More file actions
52 lines (40 loc) · 1.96 KB
/
day8_caesar_cipher.py
File metadata and controls
52 lines (40 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
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").upper()
shift = int(input("Type the shift number:\n"))
def caesar(original_text, shift_amount, cipher_direction):
cipher_text = ""
if cipher_direction == "decode":
shift_amount *= -1
for letter in original_text:
if letter not in alphabet:
cipher_text += letter
else:
shifted_position = alphabet.index(letter) + shift_amount
shifted_position = shifted_position % len(alphabet)
cipher_text += alphabet[shifted_position]
print(f"The {cipher_direction}d text is {cipher_text}")
caesar(original_text=text, shift_amount=shift, cipher_direction=direction)
# def encrypt(original_text, shift_amount):
# cipher_text = ""
# for letter in original_text:
# shifted_position = alphabet.index(letter) + shift_amount
# shifted_position = shifted_position % len(alphabet)
# cipher_text += alphabet[shifted_position]
# print(f"new index {shifted_position} and it is a letter {cipher_text}")
# encrypt(original_text=text, shift_amount=shift)
# def decrypt(original_text, shift_amount):
# cipher_text = ""
# for letter in original_text:
# shifted_position = alphabet.index(letter) - shift_amount
# shifted_position = shifted_position % len(alphabet)
# cipher_text += alphabet[shifted_position]
# print(f"new index {shifted_position} and it is a letter {cipher_text}")
# encrypt(original_text=text, shift_amount=shift)
should_continue = True
while should_continue:
caesar(original_text=text, shift_amount=shift, cipher_direction=direction)
result = input("Type 'yes' if you want to go again. Otherwise type 'no'.\n")
if result == "no":
should_continue = False