Develop and test a Python program that allows a user to type in a message and have it converted into Morse code, and also enter a Morse code and have it converted back to the original message. The encoding of Morse code is given below.
Answers
Answer:
#Function to encrypt the string
#according to the morse code chart
def encrypt(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+=MORSE_CODE_DICT[letter]+ ' '
else:
#1 space indicates different characters
#and 2 indicated different words
cipher+=' '
return cipher
#Function to decrypt the string
#From morse to English
def decrypt(message):
#extra space added at the end to access the
#last morse code
message+= ' '
decipher= "
citext= "
for letter in message:
#counter to keep track of space
i=0
#storing morse code of a single character
citext+=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(MORSE_CODE_DICT.keys())
[list(MORSE_CODE_DICT.values()).index(citext)]
citext= "
return decipher
#driver function to run the program
def main():
message="Lalchand Mail"
result=encrypt(message.upper())
print('Morse Code of ' message.upper(),':'result)
#excutes the main function
if__name__=='__main__':
process finishes with exit code 0