-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.py
More file actions
33 lines (26 loc) · 1.18 KB
/
Copy pathcaesar.py
File metadata and controls
33 lines (26 loc) · 1.18 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
#caesar cipher encrypt and decrypt
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']
def caesar(original_text, shift_amount, encode_or_decode):
output_text = ""
if encode_or_decode == "decode":
shift_amount *= -1
for letter in original_text:
#if the user enters a number/symbol/space it is added as it is to result
if letter in alphabet:
shifted_position = alphabet.index(letter) + shift_amount
shifted_position %= len(alphabet)
print(shifted_position)
output_text += alphabet[shifted_position]
else:
output_text+=letter
print(f"Here is the {encode_or_decode}d result: {output_text}")
restart=True
while restart:
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n").lower()
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
caesar(original_text=text, shift_amount=shift, encode_or_decode=direction)
print("to continue enter 'yes'\nto end enter 'no'")
restart=input()
if restart=="no":
restart=False