Report this

What is the reason for this report?

Morse Code Decoder Fails on Consecutive Spaces and Special Characters

Posted on May 13, 2026

I’m building a Morse code encoder/decoder in Python and ran into an issue when decoding messages that contain multiple spaces or unsupported symbols.

The decoder works fine for simple inputs like:

… . .-… .-… —

which correctly outputs: HELLO

But when the input contains word separators or unexpected characters, the output becomes inconsistent.

Example problematic input: … . .-… .-… — .-- — .-. .-… -… @

Expected output: HELLO WORLD

Actual behavior:

  • Extra spaces create empty decoded characters
  • Unsupported symbols like @ break the parser
  • Sometimes I get KeyError exceptions

Current decoding logic:

MORSE_CODE_DICT = { ‘.-’: ‘A’, ‘-…’: ‘B’, # … }

decoded = “”

for code in morse_input.split(" "): decoded += MORSE_CODE_DICT[code]

Questions:

What’s the best way to safely handle invalid Morse sequences? How should multiple spaces be interpreted when separating words? Is there a more robust parsing strategy for Morse code decoding in Python?

Any suggestions or best practices would be appreciated.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

Dark mode is coming soon.