Project 6: Morse Code Messenger πŸ“‘ΒΆ

Difficulty: 🟒 Beginner

Run cells top to bottom. Each step builds on the previous one and saves real files under outputs/morse_code_messenger/.

← Back to index

DescriptionΒΆ

Encode messages into International Morse Code and visualize the dot-dash pattern β€” the original digital protocol.

Libraries & ModulesΒΆ

  • Built-in: dict, str methods
  • Optional: winsound.Beep (Windows audio)

Python Concepts You'll PracticeΒΆ

Dictionaries, string processing, list comprehensions

πŸŽ›οΈ Parameter VariationsΒΆ

  • MESSAGE β€” any A-Z 0-9 string
  • DOT / DASH β€” try '.'/'-' vs '●'/'━'
  • PLAY_SOUND β€” True on Windows for beeps
  • WPM β€” words per minute (affects beep timing)
InΒ [1]:
# Install dependencies for this project (safe to re-run)
import sys
!{sys.executable} -m pip install pillow numpy matplotlib wordcloud qrcode[pil] python-barcode fpdf2 jinja2 folium plotly pandas scipy ipywidgets -q
Skipping pip install (already installed)
InΒ [2]:
# Interactive Jupyter setup β€” run this cell first
%matplotlib inline

from pathlib import Path
from IPython.display import display, Image as IPImage, HTML, Markdown, Audio, IFrame

OUTPUT_DIR = Path("outputs") / "morse_code_messenger"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
print(f"βœ… Outputs folder: {OUTPUT_DIR.resolve()}")
βœ… Outputs folder: C:\Users\Hansel Yan\Projects\CodeItAll\outputs\morse_code_messenger

Step 1 β€” Morse lookup table & encoderΒΆ

InΒ [3]:
MORSE = {
    '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': '----.',
}

# πŸŽ›οΈ TWEAK THESE
MESSAGE = "CODE IT ALL"
DOT, DASH = "●", "━"
PLAY_SOUND = False  # set True on Windows
WPM = 20

def encode(text):
    text = text.upper()
    words = []
    for word in text.split():
        letters = [MORSE.get(ch, '?') for ch in word if ch.isalnum()]
        words.append(" ".join(letters))
    return " / ".join(words)

encoded = encode(MESSAGE)
visual = encoded.replace(".", DOT).replace("-", DASH)
print("Raw Morse:", encoded)
print("Visual:   ", visual)
Raw Morse: -.-. --- -.. . / .. - / .- .-.. .-..
Visual:    ━●━● ━━━ ━●● ● / ●● ━ / ●━ ●━●● ●━●●

Step 2 β€” Save & optional audioΒΆ

InΒ [4]:
out = OUTPUT_DIR / "morse_message.txt"
out.write_text(f"Message: {MESSAGE}\nMorse: {encoded}\nVisual: {visual}\n", encoding="utf-8")
print(f"Saved to {out}")

if PLAY_SOUND:
    try:
        import winsound
        unit = int(1200 / WPM)
        for ch in encoded:
            if ch == '.':
                winsound.Beep(800, unit)
            elif ch == '-':
                winsound.Beep(800, unit * 3)
            elif ch == ' ':
                import time; time.sleep(unit / 1000)
        print("πŸ”Š Playback complete")
    except Exception as e:
        print(f"Audio skipped: {e}")
Saved to outputs\morse_code_messenger\morse_message.txt

βœ… Project completeΒΆ

Check your output folder: outputs/morse_code_messenger/

Next: Project 7: Haiku Composer Β· Index