Question
PYTHON In this step we'll be translating a single letter to Morse Code and a single Morse Code to a letter. The only input validation
PYTHON In this step we'll be translating a single letter to Morse Code and a single Morse Code to a letter. The only input validation will be on the menu selection to handle the case where the user has entered something other than 0, 1 or 2. We'll import a module of our own (provided for you) and we'll use the dictionaries in that module to do the translation. 1. Download the morse module to your folder. 2. Create your step_1.py program and import morse --> morse.pyDownload morse.py 3. Create your menu driven interface using a while loop with the following menu: Morse Code Translator 0: Exit 1: Translate a word into Morse Code 2: Translate Morse Code to text. 4. Use an if-elif-else construct, with 'else' explaining to the user that the selection was invalid and that only 0, 1 and 2 are acceptable inputs. 5. If the user enters "0", exit the program. 6. If the user enters "1": Prompt the user for a letter with the input() command and convert it to upper case with the upper() string method. Use that letter as a key in the alpha_to_morse dictionary (from the morse module) to fetch the Morse Code value. Display a message to the user showing the Morse Code associated with the letter provided. 7. If the user enters "2": Prompt the user for a Morse Code with the input() Use the Morse Code as a key to the morse_to_alpha dictionary (morse module) to fetch the corresponding letter value. Display a message to the user showing the letter associated with the Morse Code. Step 2: 25 points Now that you have the while loop and the dictionary translations working, the next task is to create two functions, translate_to_morse() and translate_to_alpha(). Make a copy of step_1. py and save it as step_2.py, as a starting point. At the top of the program, underneath the header comments, create both functions. translate_to_morse() should have one parameter, a string which consists of a letter. Move the code used in Menu Selection 1 to translate the letter to a Morse Code into the function. Put the resulting Morse Code into the return statement. In the same fashion, create the translate_to_alpha() function and move the translation code from the while loop into the function. Note that the parameter for translate_to_alpha() is a Morse Code, not a letter. In Menu Selection 1, call the translate_to_morse() function, passing the letter as an argument and storing the return value in a variable. Both the input() and print() statements from Step 1 should remain inside the while loop an should NOT be part of the function. The function's job is to do the translation, not to communicate with the user. The changes to Menu Selection 2 are basically the same. Call the translate_to_alpha() function, passing the Morse Code as an argument and storing the return value in a variable. Similarly, the input() and print() statements should not be part of the function. Note that for testing purposes, you can take the output of Menu Selection 1 and copy-and-paste it as input to Menu Selection 2 and the result should be the results should mirror each other. That is, entering "S" for Menu Selection 1 should result in "..." as a Morse Code. Use this as input for Menu Selection 2 and the result should be "S". Step 3: 25 Points At this point, the while loop and functions should be working for a single character and a single Morse Code. In this step we're going to expand the functionality to include a complete word and it's associated Morse Code. With the exception of the wording of the prompts displayed to the user in the while loop, the changes to the program should be limited to the functions. Make a copy of step_2.py and name it step_3.py and use this as a starting point. For translate_to_morse, the parameter is still a string. But instead of the string consisting of a single character it consists of a single word. You can assume that the user is very diligent and only provides an input of letters - no spaces, no numbers, no special characters. Handling invalid input is for extra credit and what you would need to do for that is described later. Inside the function, you need to add a list variable and a for loop that steps through the word one letter at a time. As before, translate the letter to a Morse Code, but add the Morse code to the list. Once the for loop completes, convert the list to a string using the join() method. Return the resulting string. For translate_to_alpha, the input is a string of Morse Codes, separated by spaces. You can assume that the user has been diligent and has only submitted input consisting of spaces, periods and hyphens. Again, input validation is for extra credit and what you need to do is described later. Inside the function, create a list for letters and a for loop. Before the for loop, call the split() method to split the string of Morse Codes into a list of Morse Codes, using a space as a delimiter. Use the for loop to step through the list of Morse Codes, translating each into a letter and storing it in the letters list. Once the for loop is complete, use the join() method to convert the list of letters to a string and return the result. Test in the same way as you did for Step 2, copying the Morse Code output of Step 1 and using it as input for Step 2.
alpha_to_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": "--.." } morse_to_alpha = { ".-": "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" }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started