forked from ArshadAman/Morse_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
morse_code.py
78 lines (69 loc) · 2.4 KB
/
morse_code.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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':'-----', ', ':'--..--', '.':'.-.-.-',
'?':'..--..', '/':'-..-.', '-':'-....-',
'(':'-.--.', ')':'-.--.-'}
#morse code provided in dictionary. Dictionary is a non-primitive data type in python
MORSE_INVERTED = {value: key for key, value in MORSE_CODE_DICT.items()}
MORSE_INVERTED[""] = " "
# function for encryption
# input in text
def encrypt(text):
morse_code = ""
for letter in text:
if letter != " ":
morse_code = morse_code + MORSE_CODE_DICT[letter]+" "
else:
morse_code += " "
return morse_code
# function for decryption
# output in morse code
def decrypt(morse_code):
text = ""
morse_text=""
morse_list = []
morse_code+= " "
for morse in morse_code:
if morse != " ":
morse_text += morse
else:
morse_list.append(morse_text)
morse_text = ""
for item in morse_list:
text += MORSE_INVERTED[item]
return text.title()
def main():
print()
message = input("Enter your message: ").upper()
print()
e_or_d = input("Encryption(e) or decreption(d): ").lower()
print()
if e_or_d == "e":
print(f"Here is the Morse Code: {encrypt(message)}\n")
elif e_or_d == "d":
print(f"Here is the plain text: {decrypt(message)}\n")
else:
print("Invalid Input\n")
main()
if __name__ == "__main__":
main()
while True:
run_again = input("Do you want to run program again. Press any to run again (q to quit): ")
run_again = run_again.lower()
run_again = run_again.trim()
print()
if run_again == "q":
print("Thank you for using me.\n")
break
else:
main()