forked from mckrd/py_repo_beginner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt_decrypt.py
47 lines (40 loc) · 1.36 KB
/
encrypt_decrypt.py
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
# Encrypts/ decrypts given input string based on user preference
def encrypt_message(message: str) -> str:
result = ''
for ch in message:
if ch.isalpha():
result += ch.swapcase()
elif ch.isnumeric():
result += str(int(ch) + 1)
else:
result += ch
return result
def decrypt_message(message: str) -> str:
result = ''
for ch in message:
if ch.isalpha():
result += ch.swapcase()
elif ch.isnumeric():
result += str(int(ch) - 1)
else:
result += ch
return result
if __name__ == "__main__":
# loop till user opts exit
while(True):
# ask for user preference
pref = input("\n".join(['e - Encrypt message',
'd - Decrypt message',
'x - Exit program',
'Enter your preference (e/d/x):']))
# user opted for exit
if pref == 'x':
break
# get input string from user
s = input('Please provide the string: ')
if pref == 'e': # invoke encryption algorithm
print('Encrypted message: ', encrypt_message(s))
elif pref == 'd': # invoke decryption algorithm
print('Decrypted message: ', decrypt_message(s))
else:
print('ERROR: invalid input!')