-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorse_code.py
More file actions
121 lines (101 loc) · 4.43 KB
/
morse_code.py
File metadata and controls
121 lines (101 loc) · 4.43 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Name: Prashant Bhandari
#A morse code encoder/decoder
# A Morse code in tuple
MORSE_CODE = (
("-...", "B"), (".-", "A"), ("-.-.", "C"), ("-..", "D"), (".", "E"), ("..-.", "F"), ("--.", "G"),
("....", "H"), ("..", "I"), (".---", "J"), ("-.-", "K"), (".-..", "L"), ("--", "M"), ("-.", "N"),
("---", "O"), (".--.", "P"), ("--.-", "Q"), (".-.", "R"), ("...", "S"), ("-", "T"), ("..-", "U"),
("...-", "V"), (".--", "W"), ("-..-", "X"), ("-.--", "Y"), ("--..", "Z"), (".-.-.-", "."),
("-----", "0"), (".----", "1"), ("..---", "2"), ("...--", "3"), ("....-", "4"), (".....", "5"),
("-....", "6"), ("--...", "7"), ("---..", "8"), ("----.", "9"), ("-.--.", "("), ("-.--.-", ")"),
(".-...", "&"), ("---...", ":"), ("-.-.-.", ";"), ("-...-", "="), (".-.-.", "+"), ("-....-", "-"),
("..--.-", "_"), (".-..-.", '"'), ("...-..-", "$"), (".--.-.", "@"), ("..--..", "?"), ("-.-.--", "!")
)
# Converting tuple to dictionary
dictionary = dict((i, j) for j, i in MORSE_CODE)
# This function is used to print intro
def print_intro():
print("Welcome to the morse \nThis program encodes and decodes Morse code.")
return 0
# This function is used to get all inputs
def get_input():
check = input("Would you like to encode (e) or decode (d) : ")
if check!='e' and check!='d' :
#if user doesn't choose e or d in above input , it prompts Invalid mode.
print("Invalid mode")
elif check=='e'or check=='E':
#if user choose small e or capital E, then it runs encode mode.
en_message=str(input("What message would you like to encode:")).upper() #accept english text
encrypt_output= encode(en_message.upper()) #it converts inputed english text to morse code
print (encrypt_output) #prints encripted output
elif check=='d'or check=='D':
#if user choose small d or capital D, then it runs decode mode.
de_message=input("What message would you like to decode:") #accept morse code
decrypt_output = decode(de_message) #it converts inputed morse code to english text
print (decrypt_output) #prits decrypted output
#This function continue program till user type n
def get_continue():
another=str(input("Would you like to encode/decode another message? (y/n):"))
while another=='y':
# get_input() and get_continue() functions continue when user type y when above input prompt up.
get_input()
get_continue()
break
if another=='y' or another!='n':
#if user type y and type others letter(not n) again get continue function runs.
get_continue()
if another=='n':
#program ends when user type n
print("Thanks for using the program, goodbye!")
quit() #program ends
# This function is used to encrypt
# English to morse code
def encode(message):
cipher = ''
for letter in message:
if letter != ' ':
# Looks up the dictionary and adds the
# corresponding morse code
# along with a space to separate
# morse codes for different characters
cipher += dictionary[letter] + ' '
else:
# 1 space indicates different characters
# and 2 indicates different words
cipher += ' '
return cipher.strip()
# This function is used to decrypt
# Morse code to Englishdef decrypt(message):
def decode(message):
message += ' '
decipher = ''
code_text = ''
for letter in message:
# checks for space
if (letter != ' '):
# counter to keep track of space
i = 0
# storing morse code of a single character
code_text += letter
# in case of space
else:
# if i = 1 that indicates a new character
i += 1
# if i = 2 that indicates a new word
if i == 2 :
# adding space to separate words
decipher += ' '
else:
# accessing the keys using their values (reverse of encryption)
decipher += list(dictionary.keys())[list(dictionary
.values()).index(code_text)]
code_text = ''
return decipher
#main funtion
def main():
print_intro() #this funtion executes in first
get_input() #this funtion executes in second
get_continue() #this funtion executes in third
#program begins from here
if __name__ == '__main__':
main() #executes main funtion