-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeWriterV1.py
More file actions
59 lines (49 loc) · 1.83 KB
/
CodeWriterV1.py
File metadata and controls
59 lines (49 loc) · 1.83 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
# *** Message Encoder / Decoder Version 1.0 (Caesar Cipher)*** #
num_to_letter = dict(zip(range(26), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
letter_to_num = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ", range(26)))
# Get the users decision: encode or decode
def user_decision():
while True:
print()
print("Would you like to Encode a message or Decode?")
print()
user_choice = input("Enter e / d: ").strip().lower()
if user_choice == "e" or user_choice == "d":
return user_choice
else:
print("Please enter a valid input (e / d).")
# Encode
def initiate_encoder():
plaintext_encode = input("Enter the message you would like to decode: ")
print()
key = int(input("Enter the key for this message (1 - 26): "))
cipher_message = ""
for c in plaintext_encode.upper():
if c.isalpha():
cipher_message += num_to_letter[(letter_to_num[c] + key)%26]
else:
cipher_message += c
return cipher_message
# Decode
def initiate_decoder():
plaintext_decode = input("Enter the message you would like to decode, as well as the key for this message (1 - 26): ")
print()
key = int(input("Enter the key for this message (1 - 26): "))
decipher_message = ""
for c in plaintext_decode.upper():
if c.isalpha():
decipher_message += num_to_letter[(letter_to_num[c] - key)%26]
else:
decipher_message += c
return decipher_message
# main function to run encode or decode functions
def main ():
user_choice = user_decision()
if user_choice == "e":
result = initiate_encoder()
elif user_choice == "d":
result = initiate_decoder()
else:
result = "Invalid Choice"
print("Message:", result)
main()