forked from CodeYouOrg/caesar-cipher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher_final.py
More file actions
44 lines (36 loc) · 1.33 KB
/
cipher_final.py
File metadata and controls
44 lines (36 loc) · 1.33 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
#ENCRYPTION CODE
alphabet = "abcdefghijklmnopqrstuvwxyz"
new_message = ""
user_message = input("Enter your secret message: ").lower()
key = int(input("Enter a key (1 through 26): "))
#print(user_input)
for character in user_message:
if character in alphabet:
position = alphabet.find(character)
new_position = (position + key) % 26
new_character = alphabet[new_position]
new_message += new_character
else:
new_message += character
print("Your secret message is " + new_message)
#DECRYPTION CODE
alphabet = "abcdefghijklmnopqrstuvwxyz"
new_message = ""
user_message = input("Enter your secret message: ").lower()
key = int(input("Enter a key (1 through 26): "))
#print(user_input)
for character in user_message:
if character in alphabet:
position = alphabet.find(character)
new_position = (position - key) % 26
new_character = alphabet[new_position]
new_message += new_character
else:
new_message += character
print("Your secret message is " + new_message)
# I know I can define a function to improve the
# code and reduce the repetition, but each time I
# tried, the code nevr ran through all the input
# prompts. Only the initial input prompt would work.
# Any advice on how to get started with moving this
# into a function would be much apprecaited!