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/.
DescriptionΒΆ
Encode messages into International Morse Code and visualize the dot-dash pattern β the original digital protocol.
Libraries & ModulesΒΆ
- Built-in:
dict,strmethods - Optional:
winsound.Beep(Windows audio)
Python Concepts You'll PracticeΒΆ
Dictionaries, string processing, list comprehensions
ποΈ Parameter VariationsΒΆ
MESSAGEβ any A-Z 0-9 stringDOT/DASHβ try'.'/'-'vs'β'/'β'PLAY_SOUNDβ True on Windows for beepsWPMβ 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