Difference between revisions of "Morse Code Translator"

From WLCS
Line 21: Line 21:
 
'''Part 1: Plaintext -> Morse code'''
 
'''Part 1: Plaintext -> Morse code'''
 
# Create a file named '''plain2morse.py'''
 
# Create a file named '''plain2morse.py'''
# Use [[Leet-speak Translator]] as a guide to create a program that converts from letters and numbers to Morse code (be sure to follow the guidelines above!)
+
# Bring up the Morse Code alphabet
 +
# Define a function named '''getMorseLetter(ch)''' that takes one parameter.  Inside the definition of getMorseLetter(), you will check to see what letter '''ch''' is, and then '''return''' the corresponding Morse code string.  See the example code below and complete it with the rest of the alphabet
 +
#* At the end of the '''getLetter(ch)''' function, '''return ch''' in case none of the letters matched
 +
 
 +
<syntaxhighlight lang="Python">
 +
def getLetter(ch):
 +
    if ch == "a":
 +
        return ".-"
 +
    if ch == "b":
 +
        return "-..."
 +
    # add the rest of your alphabet here
 +
</syntaxhighlight>
 +
 
 +
# After the function definition, prompt the user to enter a string and store it in a variable s
 +
# Initialize a new variable named '''morse''' to an empty string ""
 +
# Write a loop that traverses the '''s''' string
 +
## Inside the loop, translate the current letter (s[x]) by using the '''getLetter(s[x])''' AND add it to '''morse''' (HINT: you are building the morse string letter by letter)
 +
# After the loop, '''morse''' should contain a translation of every letter.  Print out '''morse'''
  
 
'''Part 2: Morse code -> plaintext'''
 
'''Part 2: Morse code -> plaintext'''

Revision as of 11:07, 15 February 2019

Objectives:

  • You will create a string translator that converts Morse code to a alpha-numeric characters
  • You will use while loops to traverse (walk-through) a string
  • You will use if-statements to check for Morse code strings
  • You will use functions to retrieve corresponding alpha-numeric characters

References:

Translator Guidelines:

  • Dots (.), dashes (-), and spaces ( ) will be used for the code
  • A single space will separate Morse code letters (e.g. "cat" = "-.-. .- -")
  • Words are separated by 3 spaces (e.g. "fat cat" = "..-. .- -   -.-. .- -")

Directions: Part 1: Plaintext -> Morse code

  1. Create a file named plain2morse.py
  2. Bring up the Morse Code alphabet
  3. Define a function named getMorseLetter(ch) that takes one parameter. Inside the definition of getMorseLetter(), you will check to see what letter ch is, and then return the corresponding Morse code string. See the example code below and complete it with the rest of the alphabet
    • At the end of the getLetter(ch) function, return ch in case none of the letters matched
def getLetter(ch):
    if ch == "a":
        return ".-"
    if ch == "b":
        return "-..."
    # add the rest of your alphabet here
  1. After the function definition, prompt the user to enter a string and store it in a variable s
  2. Initialize a new variable named morse to an empty string ""
  3. Write a loop that traverses the s string
    1. Inside the loop, translate the current letter (s[x]) by using the getLetter(s[x]) AND add it to morse (HINT: you are building the morse string letter by letter)
  4. After the loop, morse should contain a translation of every letter. Print out morse

Part 2: Morse code -> plaintext

  1. Create a new file named morse2plain.py
  2. Define a dictionary named morse2plain which will return the plaintext letter/number that corresponds to the Morse code letter
  3. After the dictionary, prompt the user for a Morse code string and store it in a mStr
  4. Create a string variable plainStr that starts with an empty string ""
    • plainStr will store our translated plaintext string
  5. Create a string variable mLetter that starts with an empty string ""
    • You will use mLetter in the loop below to store all the dots and dashes for a single Morse code letter (because a single letter can be represented with one or more dots and dashes)
  6. Write a loop that traverses every character of mStr
    1. If the current character is not a space, then add it to mLetter
    2. Otherwise, if the current character is a space, then translate the symbols using morse2plain[mLetter], add the translated letter to plainStr, and reset mLetter
  7. After the loop, print your translated plainStr word

Additional Features:

  • Add the ability to translate multiple words of Morse code to plaintext