Trying to convert morse code input back into English.
morse_code = { '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' : "--.. ",
'0' : "----- ",
'1' : ".---- ",
'2' : "..--- ",
'3' : "...-- ",
'4' : "....- ",
'5' : "..... ",
'6' : "-.... ",
'7' : "--... ",
'8' : "---.. ",
'9' : "----. ",
' ' : " "
}
# reverse key
morse_code_reverse = {value:key for key,value in morse_code.items()}
The function trying to convert morse code input back into English
def revert_morse_code_dictionary(text):
return ''.join(morse_code_reverse.get(i) for i in text.split())
main
if __name__ == "__main__":
cont = True
while cont:
text = input("Please enter a line of morse code text. Enter an empty line to stop.\n")
print(revert_morse_code_dictionary(text))
if text != "":
print(decode_morse(text))
else:
cont = False
Why does this keep returning this error? When the input is along the lines of
-.-. .- -. -.-- --- ..- .-. . .- -.. -- .
in revert_morse_code_dictionary
return ''.join(morse_code_reverse.get(i) for i in text.split())
TypeError: sequence item 0: expected str instance, NoneType found
morse_code_reverse.get(i)andiis not a key inmorse_code_reverse, it returns None. If you usedmorse_code_reverse[i]instead, you'd get a KeyError which would tell you the key that is causing the problem. Usinggethere just makes your code harder to debug. - khelwoodsplitremoves all spaces, none of the values in your split string will be present in the reverse dictionary. You need to remove all of the spaces from the dictionary. - Tom Karzes