Morse Code – An Overview

Morse code is an internationally accepted method to communicate messages via a system of dots and dashes. It is named after Samuel Morse, the inventor of the telegraph. The reason for the acceptance of morse code worldwide lies in the fact that a system of pulses – in the form of text (dots or dashes), light (long/short flicker) or sound (long/short beep) is used to convey a short message. The most famous example is “SOS” shorthand; three dots, three dashes, and three dots (… — …). Each letter of the english alphabet has its own morse code, not exceeding four characters. As languages evolved, morse codes were developed for punctuators, numbers and even other languages.

The Grammar of Morse Code

  1. The interval of a dot/dit is one unit
  2. The interval of a dash/dah is three units
  3. The interval between parts of the same letter is one unit
  4. The interval between two letters is three units
  5. The interval between two words is seven units
Source: https://en.wikipedia.org/wiki/Morse_code

Coding the Encoder

To even begin thinking about translating morse code to and from english, the program must know the morse symbols for the alphabet, punctuation marks and numbers. We do that by creating a dictionary, where the keys are letters and values are morse symbols.

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

Then, we define a function, encode(), which prompts the user to enter a string in english to be translated to morse. Then, we create a list of all the characters in the word or sentence that the user gives, including all the spaces. We keep the spaces to try to conform to the above rules of morse code as much as possible. Then, convert all the letters to uppercase, since our dictionary’s keys are all capital letters.

After that, insert a space in between consecutive characters of the word. This is to follow rule 4 (it will leave a single space between two letters, not 3 units) and rule 5 (it will leave 3 spaces between words, not 7 units). Then, all that’s left to do is iterate through the elements of a list and print their morse equivalents, unless the element is a space, in which case we print the space itself.

def encode():
    word = []
    sent = input("Enter a letter/word/sentence: ")

    for a in range(0, len(sent)):
        letters = slice(a, a + 1)
        letter = (sent[letters])
        word.append(letter)

    word = [i.upper() for i in word]

    for z in range(1, len(word) + len(word) - 1):
        if z % 2 == 1:
            word.insert(z, " ")

    for i in word:
        if i == ' ':
            print(" ", sep="", end="")
        else:
            # print(i)
            for y in range(0, len(morse[i])):
                diordah = slice(y, y + 1)
                beep = ((morse[i])[diordah])
                print(beep, sep="", end="")

Coding the Decoder

COMING SOON

Finishing touches

Now, all thats left to do is to ask the user whether they want to encode or decode, and accordingly call either the encode() or decode() function.

choice = input("Do you want to encode or decode? (E/D) - ").upper()
while choice != "E" and choice != "D":
    choice = input("Please enter a valid option: ").upper()
if choice == "E":
    encode()
elif choice == "D":
    decode()
Encoding a simple message

Also coming soon: Caesar cipher encoder-decoder in python

Visits: 145

One Reply to “Morse Code Encoder-Decoder in Python”

Leave a Reply

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