Difference between revisions of "Morse Code Translator"

From WLCS
Line 25: Line 25:
 
'''Part 2: Morse code -> plaintext'''
 
'''Part 2: Morse code -> plaintext'''
 
# Create a new file named '''morse2plain.py'''
 
# Create a new file named '''morse2plain.py'''
# Define a function named '''getPlainLetter(m)''' which will return the plaintext letter/number that corresponds to the Morse code letter '''m'''
+
# Define a dictionary named '''morse2plain''' which will return the plaintext letter/number that corresponds to the Morse code letter
# After the function, prompt the user for a Morse code string and store it in a '''mStr'''
+
# After the dictionary, prompt the user for a Morse code string and store it in a '''mStr'''
 
# Create a string variable '''plainStr''' that starts with an empty string ""
 
# Create a string variable '''plainStr''' that starts with an empty string ""
 
#* '''plainStr''' will store our translated plaintext string  
 
#* '''plainStr''' will store our translated plaintext string  
Line 33: Line 33:
 
# Write a loop that traverses every character of '''mStr'''
 
# Write a loop that traverses every character of '''mStr'''
 
## If the current character is not a space, then add it to '''mLetter'''
 
## If the current character is not a space, then add it to '''mLetter'''
## Otherwise, if the current character is a space, then translate the symbols using '''getPlainLetter(mLetter)''' and add the translated letter to '''plainStr'''
+
## Otherwise, if the current character is a space, then translate the symbols using '''morse2plain[mLetter]''' and add the translated letter to '''plainStr'''
 
# After the loop, print your translated '''plainStr''' word
 
# After the loop, print your translated '''plainStr''' word
  
 
'''Additional Features:'''
 
'''Additional Features:'''
 
* Add the ability to translate multiple words of Morse code to plaintext
 
* Add the ability to translate multiple words of Morse code to plaintext

Revision as of 11:52, 22 April 2015

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. 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!)

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] and add the translated letter to plainStr
  7. After the loop, print your translated plainStr word

Additional Features:

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