0

I am making an English to Morse code and vise-versa program. I successfully made it translate English to Morse code but am having a trouble translating Morse code to English. Whenever I try to translate Morse code to English, it gives me a TypeError: “text = ''.join(map(trans_back.get, user_input)) TypeError: sequence item 0: expected str instance, NoneType found”.

Here is my code so far. The last section is the part of the code where I am having trouble:

translation = {
"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": "--.. ",
"1": ".---- ",
"2": "..--- ",
"3": "...-- ",
"4": "....- ",
"5": "..... ",
"6": "-.... ",
"7": "--... ",
"8": "---.. ",
"9": "----. ",
"0": "----- ",
".": ".-.-.- ",
",": "--..-- ",
"?": "..--.. ",
"!": "..--. ",
"/": "-..-. ",
"@": ".--.-. ",
" ": " "
}

user_input = input("Input english or morse code message:\n").upper()

if all(c in translation for c in user_input):
    morse = ''.join(map(translation.get, user_input))
    print(morse)
elif all(c in ".- " for c in user_input):
    print("*needs work in order to change morse to text*")
    #trans_back = {v: k for k, v in translation.items()}
    #text = ''.join(map(trans_back.get, user_input))
    #print(text)
StrangeRanger
  • 299
  • 1
  • 2
  • 10
  • Can you show for what input you got that error – Sruthi May 06 '18 at 04:43
  • 1
    `map` is going to look up each individual item in the list in with `trans_back.get`. A string is a list of single characters. So given "... ---" it will look up "." Not "... ". The former will not be in your dictionary. – TrentP May 06 '18 at 04:51
  • For context, this seems to be a follow-up question on [this answer](https://stackoverflow.com/a/50193775/1639625)... – tobias_k May 06 '18 at 22:11

1 Answers1

0

The difference between the two blocks is that the first block expects a character-by-character mapping, but the second block requires a block of characters to be mapped to a single key. You need to split user_input along the spaces so map will apply the function to a group of .s and -s representing a single key. Of course then you will also need to remove the spaces from your reversed dictionary lookup. Here's the code that I got to work:

elif all(c in ".- " for c in user_input):
    trans_back = {v.rstrip(' '): k for k, v in translation.items()}
    text = ''.join(map(trans_back.get, user_input.split(' ')))
    print(text)
user2100826
  • 335
  • 2
  • 3
  • 13
  • Only one question, the second to last line has `user_input.split(‘ ‘)`. If I remove it, it translates into a different word: `.-` would be `A` but will then translate to `ET`. Why? – StrangeRanger May 06 '18 at 05:25
  • Because instead of iterating letter by letter, it's iterating characer by character again. Morse for E is a single character `.` and T is `-`. – user2100826 May 06 '18 at 11:21
  • Oh, ok. Thank you for the help and answering my question. – StrangeRanger May 06 '18 at 18:38