Morse code translator in Python
Morse code is a communication system that uses a series of dots and dashes to represent letters, numbers, and other characters. In this tutorial, we will create a simple Morse code translator using Python.
Step 1: Define Morse code dictionary
The first step is to define a dictionary that maps each letter, number, and symbol to its corresponding Morse code representation. Here’s an example dictionary:
morse_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': '--..', '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', '.': '.-.-.-', ',': '--..--', '?': '..--..', '/': '-..-.', '-': '-....-', '(': '-.--.', ')': '-.--.-', ' ': ' '}
Step 2: Define the Morse code translator function
Next, we will define a function that takes a string as input and returns its Morse code representation. Here’s an example function:
def morse_code_translator(text):
morse_text = ''
for letter in text:
morse_letter = morse_dict.get(letter.upper())
if morse_letter:
morse_text += morse_letter + ' '
else:
morse_text += ' '
return morse_text
This function loops through each letter in the input string, looks up its Morse code representation in the dictionary, and adds it to the output string. If the letter is not in the dictionary, it adds a space instead.
Step 3: Test the Morse code translator function Now that we have defined the Morse code translator function, let’s test it out:
text = 'SOS'
morse_text = morse_code_translator(text)
print(morse_text) # Output: '... --- ... '
This should output the Morse code representation of the input string ‘SOS’.
Step 4: Decode Morse code back to text
We will define a function that takes a Morse code string as input and returns its text representation. Here’s an example function:
def morse_code_decoder(morse_text):
text = ''
morse_letters = morse_text.split(' ')
for morse_letter in morse_letters:
letter = [key for key, value in morse_dict.items() if value == morse_letter]
if letter:
text += letter[0]
else:
text += ' '
return text
This function splits the input Morse code string into individual letters, looks up each letter’s text representation in the dictionary, and adds it to the output string. If the Morse code letter is not in the dictionary, it adds a space instead.
Step 5: Test the Morse code decoder function
Let’s test the Morse code decoder function:
morse_text = '... --- ...'
text = morse_code_decoder(morse_text)
print(text) # Output: 'SOS'
This should output the text representation of the input Morse code string ‘… — …’, which is ‘SOS’.
Step 6: Putting it all together
Now that we have defined both the Morse code translator and decoder functions, we can use them to create a simple Morse code translator program. Here’s an example program:
def morse_code_translator(text):
morse_text = ''
for letter in text:
morse_letter = morse_dict.get(letter.upper())
if morse_letter:
morse_text += morse_letter + ' '
else:
morse_text += ' '
return morse_text
def morse_code_decoder(morse_text):
text = ''
morse_letters = morse_text.split(' ')
for morse_letter in morse_letters:
letter = [key for key, value in morse_dict.items() if value == morse_letter]
if letter:
text += letter[0]
else:
text += ' '
return text
while True:
choice = input('Enter 1 to translate text to Morse code, 2 to decode Morse code to text, or 3 to exit: ')
if choice == '1':
text = input('Enter text to translate to Morse code: ')
morse_text = morse_code_translator(text)
print(morse_text)
elif choice == '2':
morse_text = input('Enter Morse code to decode to text: ')
text = morse_code_decoder(morse_text)
print(text)
elif choice == '3':
break
else:
print('Invalid choice.')
This program presents the user with a menu where they can choose to translate text to Morse code, decode Morse code to text, or exit the program. Depending on the user’s choice, the program prompts them to enter the text or Morse code to translate/decode, calls the appropriate function, and prints the result.
In this post, we created a simple Morse code translator using Python. We defined a dictionary that maps each letter, number, and symbol to its corresponding Morse code representation, and used it to create a function that translates text to Morse code and another function that decodes Morse code to text. We then used these functions to create a simple command-line program that allows the user to translate/decode text to/from Morse code.