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()

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

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”.

This is the code I have so far:

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”
}

morsecode_translator = “””
Morse Code Translator

0: Exit
1: Translate a word into Morse Code
2: Translate Morse code to text
“””

while True:
print(morsecode_translator)

choice = int(input(“Enter your Choice: “))

if choice == 0:
print(“Exiting Program…”)
break

elif choice == 1:

letter = input(“Please Input the Letter: “)

letter = letter.upper()

morse_code = alpha_to_morse.get(letter)

print(“Morse Code associated with the letter”,letter,”is”, morse_code)

elif choice == 2:

morse_code = input(“Please Input the Morse Code: “)

letter = morse_to_alpha.get(morse_code)

print(“Letter associated with the Morse Code”,morse_code,”is”, letter)

else:
print(“The selection was invalid and that only 0, 1, and 2 are acceptable inputs.”)

continue

Complete Answer:

Get Instant Help in Homework Asap
Get Instant Help in Homework Asap
Calculate your paper price
Pages (550 words)
Approximate price: -
Open chat
1
Hello 👋
Thank you for choosing our assignment help service!
How can I help you?