0
votes

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
1
When you call morse_code_reverse.get(i) and i is not a key in morse_code_reverse, it returns None. If you used morse_code_reverse[i] instead, you'd get a KeyError which would tell you the key that is causing the problem. Using get here just makes your code harder to debug. - khelwood
All of your Morse code strings end in a space. Since split removes 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
@TomKarzes I was incredibly slow in writin my answer from my phone, so I didn't notice your comment. If you wish I'll delete my answer so that you can write yours. - Roberto Caboni
@RobertoCaboni It's ok, I don't mind. Thanks for asking. - Tom Karzes

1 Answers

0
votes

In

def revert_morse_code_dictionary(text):
     return ''.join(morse_code_reverse.get(i) for i in text.split())

your first action is splitting the input string, so that you have

["-.-.", ".- -.", "-.--"...]

All spaces are removed by the split action. But all the elements in your dictionary contain a space in the end. For example

"C": "-.- ."

will not match the first item -.-.. Since the latter won't match any key, None will be returned by get().