Loading

Morse code is a system of communication that uses a series of dots and dashes to represent letters, numbers, and punctuation. Developed in the mid-1800s by Samuel Morse and his assistant Alfred Vail, Morse code was initially used for telegraph communication. Today, it is still used by amateur radio operators, aviators, and military personnel to communicate.

In this three-part article, we will build a Python application that accepts text input from the user and converts it to Morse code. This article will focus on creating a console application to convert text to Morse Code. The second and third parts will continue this theme and add a GUI (Graphical User Interface) and sound to the project. This project can be a fun and educational project that will give you a better understanding of Morse code and Python programming.

Getting Started

First, let’s create a new Python file called “morse_code.py”. We will begin by defining a dictionary that maps each letter, number, and punctuation mark to its corresponding Morse code representation:

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': '--..', '0': '-----', '1': '.----', '2': '..---', 
             '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', 
             '8': '---..', '9': '----.', ',': '--..--', '.': '.-.-.-', '?': '..--..', 
             '/': '-..-.', '-': '-....-', '(': '-.--.', ')': '-.--.-'}

Next, we will define a function called “convert_to_morse” that takes a string as input and returns the corresponding Morse code representation. This function will loop through each character in the input string, look up its Morse code representation in the dictionary we defined earlier, and concatenate the results:

def convert_to_morse(text):
    morse_code = ''
    for char in text.upper():
        if char in CODE_DICT:
            morse_code += CODE_DICT[char] + ' '
        else:
            morse_code += ' '
    return morse_code

Finally, we will add some code to accept input from the user, call the “convert_to_morse” function, and print the result:

if __name__ == '__main__':
    text = input('Enter text to convert to Morse code: ')
    morse_code = convert_to_morse(text)
    print(morse_code)

Putting it All Together

Here is the complete code for our Python console application:

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': '--..', '0': '-----', '1': '.----', '2': '..---', '3': '...--', 
             '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', 
             '9': '----.', ',': '--..--', '.': '.-.-.-', '?': '..--..', '/': '-..-.', 
             '-': '-....-', '(': '-.--.', ')': '-.--.-'}

def convert_to_morse(text):
    morse_code = ''
    for char in text.upper():
        if char in CODE_DICT:
            morse_code += CODE_DICT[char] + ' '
        else:
            morse_code += ' '
    return morse_code

if __name__ == '__main__':
    text = input('Enter text to convert to Morse code: ')
    morse_code = convert_to_morse(text)
    print(morse_code)

When you run the “morse_code.py” file in your terminal, you will be prompted to enter text to convert to Morse code. Once you enter your text, the program will output the corresponding Morse code representation.

Conclusion

In this article, we have built a simple Python console application that converts text to Morse code. We have also learned a bit about the history of Morse code and its continued use today. In the next article in this series, we will explore how to create a graphical user interface for our Morse code converter using the PyQT5 library.

Add Comment

Your email address will not be published. Required fields are marked *