
Shift the Message
Julius Caesar, a Roman general, hid his letters with a simple
trick: move every letter a few places along the alphabet. With a
shift of 3, a becomes d, and pip becomes sls.
The key is the size of the shift. Both friends must know it.
Wrapping around with %
What happens to z with a shift of 1? Its position is 25, plus
1 is 26, but there is no letter 26! We want to wrap around
back to a.
The remainder operator % does exactly that. n % 26 is what is
left over after dividing by 26, so it is always 0 to 25.
print(26 % 26) # 0 -> "a"
print(28 % 26) # 2 -> "c"
print(5 % 26) # 5 (small numbers stay the same)
So shifting a letter is three steps: letter to position, add the
shift and wrap with % 26, then position back to letter.
Assignment
Fill in the three ___ blanks.
shift_letter(letter, shift)wraps the new position with% 26and turns it back into a letter.encode(word, shift)builds the secret word by shifting every letter withshift_letter.
The program should print sls.
Fill in the three ___ blanks.
shift_letter(letter, shift)wraps the new position with% 26and turns it back into a letter.encode(word, shift)builds the secret word by shifting every letter withshift_letter.
The program should print sls.
Press Run to try your code, then Check to test it.