Back to Contents Page

Lesson: Converting Text to Morse Code using Python

By now, you should have learned about Morse code and the basics of Python. For this lesson, the task is to create a program in Python that takes in a string and outputs a Morse code sequence. It's a lot simpler than it seems!

Python Dictionaries

First, we would need to write the conversion table for converting text to their Morse code equivalent using a Python dictionary.

In Python, a dictionary is a data structure similar to a list. However, instead of mapping values to indexes (0,1,2,3,...) like in a list, dictionaries have keys and values. Each value is mapped to a unique key.

To create a dictionary, we use the following syntax (for example):

myDict = { "one" : "I", "two" : "II", "three" : "III", "four" : "IV", "five" : "V" }

This creates a dictionary myDict consisting of the key "one" associated with the value "I", the key "two" associated with the value "II", and so on.

To access a value from the dictionary, we use the following syntax:

myDict[var]

This checks the dictionary myDict for the key described in the variable var, and returns the value associated with the key. var could be any variable, as long as the value of the variable exists as a key in CODE. For example:

myDict = { "one" : "I", "two" : "II", "three" : "III", "four" : "IV", "five" : "V" }

var = 'one'
print(myDict[var])

or:

myDict = { "one" : "I", "two" : "II", "three" : "III", "four" : "IV", "five" : "V" }

print(myDict["one"])

Both will print out "I". As you can see from the dictionary myDict, the value associated with the key "one" is "I".

If you try to return the value of a key that does not exist in the dictionary, it will return an error:

myDict = { "one" : "I", "two" : "II", "three" : "III", "four" : "IV", "five" : "V" }

print(myDict["six"])

This will return a KeyError, as "six" is not defined as a key in myDict in the above example.

Task

Your task is to create a dictionary named CODE that contains the Morse code conversion table, with the original characters as keys and their Morse code equivalent as values. You must include all upper case letters, numbers, and symbols that has a Morse code equivalent in the dictionary.

We have written down the first few keys and values for you as examples:

CODE = { ' ': '_',
   'A' : '.-',
   'B' : '-...',
   'C' : '-.-.',
   #continue here
}

The purpose of this dictionary is that so we are able to look up the Morse code equivalent of each character using the following syntax:

CODE[character]

For example:

CODE = { ... } #your dictionary

print(CODE['A'])

or:

CODE = { ... }

character = 'A'
print(CODE[character])

Will both print out '.-'

After you have finished your dictionary CODE, try testing it adding the following code after it:

print(CODE['A'])
print(CODE['B'])
print(CODE['C'])
etc..

and check whether it prints the correct Morse code values or not.

Convert input string to Morse code

Now, we will use said dictionary CODE to convert input text into Morse code. Your program should be able to produce output similiar to:

Your task now would be to create a function convertToMorseCode(sentence) that returns the Morse code equivalent of an input sentence, such that the following code will print out the input text converted into Morse code:

CODE = { ... } #insert your dictionary CODE from the previous section

def convertToMorseCode(sentence):
   #your code here
   return encodedSentence

sentence = input("Enter sentence: ")
encodedSentence = convertToMorseCode(sentence)
print(encodedSentence)

These following functions/techniques will be useful for your task:

.upper() -> Converts all characters in a string to upper case. We use this to convert our input string to all uppercase characters, as our dictionary only contains uppercase characters as key. For example:

text = "asdf"
text.upper()
print(text)

This will print out "ASDF".

Also, remember that a string is basically a list of characters, and you can iterate over them using a for loop, for example:

sentence = "a string"
for character in sentence:
   print(character)

You can use the increment operator += to append another string to the end of a string, for example:

sentence = "qwerty" sentence += "uiop" print(sentence)

This will print out "qwertyuiop".

Printing out a series of "Dash"-es and "Dot"-s

By using the function convertToMorseCode(sentence), we can create a new function that takes in a sentence and print out a series of "Dash"-es and "Dot"-s, such as:

Besides "Dash"-es and "Dot"-s, your program should print out "." after the end of every character and "Space" between every word, as shown above.

Your task is to complete the function encodeToWords(sentence) that would allow you to print out output similar to the example shown:

CODE = { ... } #insert your dictionary CODE

def convertToMorseCode(sentence):
   #your code from the previous section here

def encodeToWords(sentence):
   encodedSentence = convertToMorseCode(sentence)
   #your code here

sentence = input("Enter sentence: ")
encodeToWords(sentence)

Hint: iterate over each character in encodedSentence, and depending on what character it is, print "Dash", "Dot", ".", or "Space". if statements are very useful here.

Marking and Solution

A solution to the tasks in this lesson can be opened here. The student's program does not have to exactly match this solution, as long as it works and produces the expected output.

The functionality of the program (whether it produces the expected output) should equate to about 50% of the marks. The student should obviously recieve full marks for this section if their program always produces the correct output. Marks should be deducted for things such as the dictionary containing the wrong data, their "convertToMorseCode" function not correctly changing a sentence into morse code and their "encodeToWords" function not printing the correct set of words.

Besides that, students can be judged based on factors such as code neatness, variable naming etc. This should make up about 25% of the marks. Marks should be deducted based on whether suitable variable and function names have been used i.e names like "a" or "x" should loose marks, names like "sentence" for the sentence you want to convert are a good example. Marks can also be deducted for incorrect indentation of code. For the final 25% students may also be ranked based on the time required to complete the task (the time to produce a fully-functional version).

Students are to be judged in a competitive manner, and the top 1-5 students/groups in the class (depending on class size) should be awarded so, and students should be marked relative to the rest of the class.