-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesarCipher.py
More file actions
57 lines (43 loc) · 1.92 KB
/
caesarCipher.py
File metadata and controls
57 lines (43 loc) · 1.92 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
import art
print(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']
# def encrypt(original_text, shift_amount):
# cipher_text = ""
# for letter in original_text:
# shifted_position = alphabet.index(letter) + shift_amount
# shifted_position %= len(alphabet)
# cipher_text += alphabet[shifted_position]
# print(f"Here is the encoded result: {cipher_text}")
#
#
# def decrypt(encrypted_text, shift_amount):
# cipher_text = ""
# for letter in encrypted_text:
# shifted_position = alphabet.index(letter) - shift_amount
# cipher_text += alphabet[shifted_position]
# print(f"Your decrypted text is : {cipher_text}")
#
# encrypt(original_text=text, shift_amount=shift)
# decrypt(text,shift)
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 letter not in alphabet: #checks if character is not an alphabet
output_text += letter #adds the character as it is to the output_text. hence symbol remains as expected
continue
shifted_position = alphabet.index(letter) + shift_amount
shifted_position %= len(alphabet)
output_text += alphabet[shifted_position]
print(f"Here is your {encode_or_decode}d result: {output_text}")
should_user_continue = True
while should_user_continue:
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(text,shift,direction)
continue_or_not = input("Do you want to do this again? Type 'yes' to continue, type 'no' to end\n").lower()
if continue_or_not == "no":
should_user_continue = False
print("Goodbye then!")