-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt_improved.py
More file actions
38 lines (33 loc) · 953 Bytes
/
decrypt_improved.py
File metadata and controls
38 lines (33 loc) · 953 Bytes
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
# input text to encrypt
text = input("Enter message: ")
# enter valid shift value (repeat until it succeeds)
shift = 0
while shift == 0:
try:
shift = int(input("Enter cipher's shift (1..25): "))
if shift not in range(1,26):
raise ValueError
except ValueError:
shift = 0
if shift == 0:
print("Bad shift value!")
cipher = ''
for char in text:
# is it a letter?
if char.isalpha():
# shift its code
code = ord(char) + shift
# find the code of the first letter (upper- or lower-case)
if char.isupper():
first = ord('A')
else:
first = ord('a')
# make correction
code -= first
code %= 26
# append encoded character to message
cipher += chr(first + code)
else:
# append original character to message
cipher += char
print(cipher)