The goal here is to prevent the program from crashing due to bad input. Suppose the user enters a number as text to be converted to Morse Code? Or as part of a Morse Code to be converted to a letter? Since neither exists as a key in either dictionary, a KeyError exception will be thrown. You have two choices. You can either implement try/except to catch the exception or you can check to see if the key exists in the dictionary. If the key doesn’t exist or you catch the exception, immediately return an empty string. In the while loop, check to see if the string returned by the function has a length greater than 0, using the len() function. If it doesn’t, then display a message to the user indicating that the input was invalid and what is needed for valid input.
If you want to get fancy, a second approach to reporting a problem to the user is to return two values and use multiple assignment. One return value is the string and the second is a status. Set the status to False if there’s a problem and True if everything is good and then check the status in the while loop to determine whether to display a warning or the normal result message.
Finished my code so far but stuck on this last part, here is my code 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.append(alpha_to_morse.get(i))
return””.join(morse_code_for_string)
def translate_to_alpha(morse_value):
alpha_from_morse = []
for i in morse_value.split():
alpha_from_morse.append(morse_to_alpha.get(i))
return””.join(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