Code

The Signal Alphabet

Every secret code starts with one trick: turn letters into numbers. Numbers are easy to change with maths. Then you turn them back into letters.

Inside the computer, every character already has a code number. ord() gives you a character's number. chr() does the reverse.

print(ord("a"))   # 97
print(ord("b"))   # 98
print(chr(99))    # c
print(ord("A"))   # 65  (capitals have their own numbers)

The lowercase letters sit side by side, from 97 to 122. So if you subtract ord("a"), you get a friendly position: a is 0, b is 1, all the way to z at 25.

print(ord("d") - ord("a"))   # 3
print(chr(3 + ord("a")))     # d

Pip tip: Spies call this kind of alphabet-with-numbers a "signal alphabet". The Guild's lantern towers flash these numbers across the sea at night.

Assignment

Use lowercase letters only.

  1. letter_to_number(letter) returns the letter's position: "a" gives 0, "z" gives 25. Use ord().
  2. number_to_letter(number) does the reverse: 0 gives "a". Use chr().
  3. word_to_numbers(word) returns a list of positions, one per letter. word_to_numbers("pip") gives [15, 8, 15].