[Solved] Python: Dots and Dashes as Dictionary Keys
An example, the dictionay key for an "A" which is ".-" outputs an "E" and a "T"
The "E" correlating to the "." and the "T" to the "-"
How can you code the ".-" to output as an "A"?
Here's the code in question:
I made a morse code generator that works, (it's the base code for the problematic code above):
The decoder at the top is just a reversal of the generator's dictionary keys.
Run this code:
... - .-. .- -. --. . .-. / - .... .. -. --. ... / .---- .---- / -.- .. -.-. -.- ... / .- ... ...
Your code is fine
You just need to give it correct input and handle it in a sensible way
The change i made was splitting the user input by single spaces and pass it thru the same loop you made
Consider using the builtin maketrans() and translate() functions . Converting FROM text TO morse is relatively ease but the other way....oh ...not so sure...e.g. if an input is .--.-.-.-.-..---. how do you know where on character finishes and the next one starts (that's always puzzled me). You could probable use regular expression to pull the group of dots and dashes if a group was inputted with a space in between.
In python there's a split(separator, maxsplit) method for string literals. This method serves to separate parts of a string based on a common separator. If what you want is to translate a Morse code sentence into proper English, that's relatively simple, so long as you have that separator between Morse code "Characters".
There's various references for this method on python.org, as well as here: https://www.geeksforgeeks.org/python-string-split/.
Hope this helps.