
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.
letter_to_number(letter)returns the letter's position:"a"gives0,"z"gives25. Useord().number_to_letter(number)does the reverse:0gives"a". Usechr().word_to_numbers(word)returns a list of positions, one per letter.word_to_numbers("pip")gives[15, 8, 15].
Use lowercase letters only.
letter_to_number(letter)returns the letter's position:"a"gives0,"z"gives25. Useord().number_to_letter(number)does the reverse:0gives"a". Usechr().word_to_numbers(word)returns a list of positions, one per letter.word_to_numbers("pip")gives[15, 8, 15].
main.py Python sleeping
Press Run to try your code, then Check to test it.