-
Notifications
You must be signed in to change notification settings - Fork 472
/
morse_code_decoder_encoder.py
62 lines (56 loc) · 1.99 KB
/
morse_code_decoder_encoder.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
MORSE_CODE_DICT = { 'A':'.-', 'B':'-...',
'C':'-.-.', 'D':'-..', 'E':'.',
'F':'..-.', 'G':'--.', 'H':'....',
'I':'..', 'J':'.---', 'K':'-.-',
'L':'.-..', 'M':'--', 'N':'-.',
'O':'---', 'P':'.--.', 'Q':'--.-',
'R':'.-.', 'S':'...', 'T':'-',
'U':'..-', 'V':'...-', 'W':'.--',
'X':'-..-', 'Y':'-.--', 'Z':'--..',
'1':'.----', '2':'..---', '3':'...--',
'4':'....-', '5':'.....', '6':'-....',
'7':'--...', '8':'---..', '9':'----.',
'0':'-----', ', ':'--..--', '.':'.-.-.-',
'?':'..--..', '/':'-..-.', '-':'-....-',
'(':'-.--.', ')':'-.--.-'}
def encrypt(message):
"""Encrypt the message to Morse code."""
cipher = ''
for letter in message:
if letter in MORSE_CODE_DICT: # Check if letter is valid
cipher += MORSE_CODE_DICT[letter] + ' '
else:
print(f'This: {letter} could not be converted into Morse code, removing it from the message')
return cipher.strip() # Remove trailing space
def decrypt(message):
message += ' '
decipher = ''
citext = ''
for letter in message:
if (letter != ' '):
i = 0
citext += letter
else:
i += 1
if i == 2 :
decipher += ' '
else:
decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT
.values()).index(citext)]
citext = ''
return decipher
switch = input("Enter 1 for encryption and 2 for decryption: ")
if switch == '1':
message = input("Enter the message you want to encrypt(press enter for default input): ")
if not message:
print("No inputs detected so we are using default input 'Rohit'")
message = 'Rohit'
result = encrypt(message.upper())
print (result)
else:
message = input("Enter the message you want to decrypt(press enter for default message): ")
if not message:
print("No inputs detected so we are using default input '.--. -.-- - .... --- -.'")
message = '.--. -.-- - .... --- -.'
result = decrypt(message)
print (result)