At this point, the while loop and functions should be working for a single character and a single Morse Code

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.

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, display 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.

This is the code I have so far:

#
# SEC290.13767.FA2022
# Tyler Row
# 9/30/22
# Programming Assignment 5
#

def translate_to_morse(string_value):

morse_code_for_string = “”

for i in string_value:

morse_code_for_string += alpha_to_morse.get(i) if morse_code_for_string == “” else ” ” + alpha_to_morse.get(i)

return morse_code_for_string

def translate_to_alpha(morse_value):

alpha_from_morse = “”

for i in morse_value.split():

alpha_from_morse += morse_to_alpha.get(i)

return alpha_from_morse

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:

string = input(“Please enter a string: “)

string = string.upper()

morse_code = translate_to_morse(string)

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

elif choice == 2:

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

string = translate_to_alpha(morse_code)

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

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?