Morse code is a communication system that uses a series of dots, dashes, and pauses to represent letters, numbers, and symbols. It was developed in the 1830s by Samuel Morse and Alfred Vail for the telegraph, which transmitted electrical signals over wires.
Morse code can be transmitted using sound, light, or electrical signals, and it is still used today in certain contexts, such as in aviation and amateur radio.
Each letter, number, and symbol represents a unique sequence of dots and dashes. For example, “A” is represented by a single dot followed by a dash , while “B” is represented by a dash followed by three dots .
The length of the dots and dashes and the pauses between them are standardized to send and receive messages efficiently and accurately.
We need to implement the following three core functionalities to make a Morse code translator in Python:
Define a mapping of characters.
Encode a message in Morse code.
Decode a message from a Morse code sequence.
We define a dictionary that maps letters, numbers, and symbols to their respective Morse code sequences.
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': '----.', '.': '.-.-.-', ',': '--..--',
'?': '..--..', "'": '.----.', '!': '-.-.--', '/': '-..-.', '(': '-.--.', ')': '-.--.-', '&': '.-...',
':': '---...', ';': '-.-.-.', '=': '-...-', '+': '.-.-.', '-': '-....-', '_': '..--.-', '"': '.-..-.',
'$': '...-..-', '@': '.--.-.', ' ': '/'
}
The morse_dict
dictionary has the following properties:
A combination of the .
and -
characters denote different letters, numbers, and symbols.
The /
character denotes a separator between two words.
We define a function that takes a message (a string) as input and returns the Morse code sequence for that message.
For example, the message Hello World
will be converted to
.... . .-.. .-.. --- / .-- --- .-. .-.. -.. -.-.--
.
Note: There is a space (
" "
) after each character and a forward slash (/
) after each word in the Morse code sequence.
# Define the Morse code 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': '----.', '.': '.-.-.-', ',': '--..--','?': '..--..', "'": '.----.', '!': '-.-.--', '/': '-..-.', '(': '-.--.', ')': '-.--.-', '&': '.-...',':': '---...', ';': '-.-.-.', '=': '-...-', '+': '.-.-.', '-': '-....-', '_': '..--.-', '"': '.-..-.','$': '...-..-', '@': '.--.-.', ' ': '/'}# Convert a message to Morse code.def to_morse_code(message):morse_code = ''for char in message.upper():if char in morse_dict:morse_code += morse_dict[char] + ' 'return morse_code# Driver codedef main():message = "Hello World"print("Message: ", message, "\nMorse code sequence: ", to_morse_code(message))if __name__ == "__main__":main()
The to_morse_code
function has the following properties:
We initialize a variable, morse_code
, with an empty string. This variable will store the Morse code sequence of the message.
We convert the message into uppercase since Morse code is not case sensitive.
We traverse each character of the capitalized message.
We use each character as a key in the morse_dict
dictionary to access the corresponding value (i.e., the Morse code representation of the character). Next, we add this value and a space character to the morse_code
variable.
After the message has been traversed, the morse_code
variable contains the Morse code sequence of the message, so we return it.
We define a function that takes a Morse code sequence (a string of dots, dashes, spaces, and slashes) as input and returns the corresponding message.
For example, the Morse code sequence .. / .- -- / .- / -... --- -.-- .-.-.-
will be converted to I AM A BOY
.
# Define the Morse code 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': '----.', '.': '.-.-.-', ',': '--..--','?': '..--..', "'": '.----.', '!': '-.-.--', '/': '-..-.', '(': '-.--.', ')': '-.--.-', '&': '.-...',':': '---...', ';': '-.-.-.', '=': '-...-', '+': '.-.-.', '-': '-....-', '_': '..--.-', '"': '.-..-.','$': '...-..-', '@': '.--.-.', ' ': '/'}# Convert a Morse code sequence to a message.def from_morse_code(morse_code):message = ''morse_code = morse_code.split(' ')for code in morse_code:for char, morse in morse_dict.items():if morse == code:message += charreturn messagedef main():morse_code_sequence = ".. / .- -- / .- / -... --- -.-- .-.-.-"print("Morse code sequence: ", morse_code_sequence, "\nMessage: ", from_morse_code(morse_code_sequence))if __name__ == "__main__":main()
The from_morse_code
function has the following properties:
We initialize a variable, message
, with an empty string. This variable will store the decoded message from the Morse code sequence.
We reinitialize the morse_code
variable by applying the split
method on it with " "
as the separator. This converts the Morse code sequence to a list containing the Morse code representation of each character as a separate element.
We traverse the elements of the morse_code
list.
For each element, we traverse the morse_dict
dictionary to find the key containing the character corresponding to its Morse code representation. We add this key to the message
variable.
After the list has been traversed, the message
variable contains the decoded message, so we return it.
We implement a user interface that allows the user to choose whether to translate from text to Morse code or from Morse code to text.
# Define the Morse code 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': '----.', '.': '.-.-.-', ',': '--..--', '?': '..--..', "'": '.----.', '!': '-.-.--', '/': '-..-.', '(': '-.--.', ')': '-.--.-', '&': '.-...', ':': '---...', ';': '-.-.-.', '=': '-...-', '+': '.-.-.', '-': '-....-', '_': '..--.-', '"': '.-..-.', '$': '...-..-', '@': '.--.-.', ' ': '/' } # Convert a message to Morse code. def to_morse_code(message): morse_code = '' for char in message.upper(): if char in morse_dict: morse_code += morse_dict[char] + ' ' return morse_code # Convert a Morse code sequence to a message. def from_morse_code(morse_code): message = '' morse_code = morse_code.split(' ') for code in morse_code: for char, morse in morse_dict.items(): if morse == code: message += char return message # User interface def main(): while True: choice = input("Choose an option:\nPress 1 to convert text to Morse code\nPress 2 to convert Morse code to text\nPress -1 to quit\n") if choice == '1': message = input("Enter a message to convert to Morse code: ") morse_code = to_morse_code(message) print(morse_code) elif choice == '2': morse_code = input("Enter a Morse code sequence to convert to text: ") message = from_morse_code(morse_code) print(message) elif choice == '-1': print("Quitted successfully") break else: print("Invalid choice, please choose from the available options.") if __name__ == "__main__": main()
Free Resources