
Bug hunt
The Missing Spaces
Real messages have capital letters, spaces and punctuation. Our
first cipher only knew about a to z, so it mangles everything
else.
The bug
Pip tried to send Meet at the Dock! with a shift of 3. It came
out as one long jumble with no spaces:
jhhwqdwqwkhqarfnr
The space and ! were shifted as if they were letters, and the
capital M and D came out wrong too.
How to fix it
You can compare characters to check what kind they are:
ch = "Q"
print("a" <= ch <= "z") # False, not lowercase
print("A" <= ch <= "Z") # True, a capital
Lowercase letters count from ord("a"). Capitals count from
ord("A"). Anything else should come back unchanged.
Assignment
Fix shift_letter(ch, shift):
- Lowercase letters shift using
ord("a")as the start. - Capital letters shift using
ord("A"), so they stay capitals. - Every other character (spaces, digits, punctuation) is returned as it is.
The program should print Phhw dw wkh Grfn!.
Fix shift_letter(ch, shift):
- Lowercase letters shift using
ord("a")as the start. - Capital letters shift using
ord("A"), so they stay capitals. - Every other character (spaces, digits, punctuation) is returned as it is.
The program should print Phhw dw wkh Grfn!.
main.py Python sleeping
Press Run to try your code, then Check to test it.