-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson3.py
More file actions
67 lines (36 loc) · 1.18 KB
/
Lesson3.py
File metadata and controls
67 lines (36 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
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
60
61
62
63
64
65
66
67
def encipher(message, key):
result = "" # empty string we'll add to
for character in message:
if character.isalpha(): # if character is a letter ("alpha" as in alphabet)
ascii = ord(character) # get character's ASCII code with ord()
shifted = ascii + key
if shifted > ord('z'):
shifted -= 26
result += chr(shifted) # turn integer to character with chr()
else:
result += character # if character is not a letter, just add it to the result
return result
def decipher(message, key):
result = ""
for character in message:
if character.isalpha():
ascii = ord(character)
# What do we put here to make this function decipher our enciphered message?
result += chr(shifted)
else:
result += character
return result
# Don't worry about the code below.
command = ""
while True:
command = input()
tokens = command.split(" ")
if command == "exit":
break
func = tokens[0]
message = tokens[1]
key = int(tokens[2])
if func == "encipher":
print(encipher(message, key))
elif func == "decipher":
print(decipher(message, key))