Encrypting a message with a Caesar loop in Python

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Encrypting a message with a Caesar loop in Python

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line shifts an upper-case letter ch by shift and wraps past Z back to A?
A chr(ord(ch) + shift)
B chr((ord(ch) - 65 + shift) % 26 + 65)
2. How should a Caesar loop build up the ciphertext string?
A result += chr((ord(ch) - 65 + shift) % 26 + 65)
B result = chr((ord(ch) - 65 + shift) % 26 + 65)
3. What should the loop do with a space in the message, such as the gap in 'HI THERE'?
A Leave it unchanged and add it to the result
B Shift it by the same amount as the letters
4. Which test lets the loop skip spaces and punctuation?
A if ch.isupper() == False:
B if ch.isalpha():
5. Which is the correct first line to wrap the cipher in a reusable function?
A def caesar(text, shift):
B def caesar(text shift)

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1chr((ord(ch) - 65 + shift) % 26 + 65) Aresult += chr((ord(ch) - 65 + shift) % 26 + 65)
2if ch.isalpha(): BUses = instead of += so each letter replaces the result and only the last letter is kept
3Build up the ciphertext one character at a time by adding each new character to a result string CLeaves out % 26 so letters near the end of the alphabet do not wrap back to A and become symbols
4Wrap the loop in a function so you can encrypt any text with any shift Ddef caesar(text, shift):
5result = chr((ord(ch) - 65 + shift) % 26 + 65) EOnly shifts the character if it is a letter, so spaces and punctuation can be left unchanged
6chr((ord(ch) - 65 + shift) + 65) FShifts the upper-case letter ch forward by shift places and wraps from Z back to A

Definitions

chr((ord(ch) - 65 + shift) % 26 + 65)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
new = chr((___(ch) - 65 + shift) % 26 + 65)
2.
new = chr((ord(ch) - 65 + shift) _ 26 + 65)
3.
for ch in text: if ch._______(): result += shifted
4.
___ caesar(text, shift): result = "" return result
5.
result = "" for ch in text: result __ new

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A result = ""
B for ch in text:
C if ch.isalpha():
D n = ord(ch) - 97
E n = (n + shift) % 26
F result += chr(n + 65)
Question 2
A result = ""
B for ch in range(text):
C n = ord(ch) - 65
D n = (n + shift) % 26
E result += chr(n + 65)
F print(result)
Question 3
A def caesar(text, shift):
B result = 0
C for ch in text:
D n = ord(ch) - 65
E n = (n + shift) % 26
F result += chr(n + 65)
Question 4
A text = "HELLO"
B result == ""
C for ch in text:
D n = ord(ch) - 65 + 3
E result += chr(n % 26 + 65)
Question 5
A result = ""
B for ch in text:
C n = chr(ch) - 65 + shift
D n = n % 26
E result += chr(n + 65)

Answer Key

Encrypting a message with a Caesar loop in Python
Section A: Quick Questions
Q1
B — chr((ord(ch) - 65 + shift) % 26 + 65)
Q2
A — result += chr((ord(ch) - 65 + shift) % 26 + 65)
Q3
A — Leave it unchanged and add it to the result
Q4
B — if ch.isalpha():
Q5
A — def caesar(text, shift):
Section B: Match the Pairs
Q1
chr((ord(ch) - 65 + shift) % 26 + 65) → Shifts the upper-case letter ch forward by shift places and wraps from Z back to A
Q2
if ch.isalpha(): → Only shifts the character if it is a letter, so spaces and punctuation can be left unchanged
Q3
Build up the ciphertext one character at a time by adding each new character to a result string → result += chr((ord(ch) - 65 + shift) % 26 + 65)
Q4
Wrap the loop in a function so you can encrypt any text with any shift → def caesar(text, shift):
Q5
result = chr((ord(ch) - 65 + shift) % 26 + 65) → Uses = instead of += so each letter replaces the result and only the last letter is kept
Q6
chr((ord(ch) - 65 + shift) + 65) → Leaves out % 26 so letters near the end of the alphabet do not wrap back to A and become symbols
Section C: Definitions
Q1
chr((ord(ch) - 65 + shift) % 26 + 65) — Shifts an upper-case letter ch along the alphabet by shift, wrapping past Z back to A. A for loop applies it to each letter and builds the ciphertext with +=, skipping spaces.
Section D: Fill in the Blanks
Q1
new = chr((ord(ch) - 65 + shift) % 26 + 65)
Q2
new = chr((ord(ch) - 65 + shift) % 26 + 65)
Q3
for ch in text: if ch.isalpha(): result += shifted
Q4
def caesar(text, shift): result = "" return result
Q5
result = "" for ch in text: result += new
Section E: Spot the Error
Q1
D — Upper-case letters start at ord('A') = 65, so subtract 65 to get a position from 0 to 25. Subtracting 97 is for lower-case letters and would give negative numbers here. It should be n = ord(ch) - 65.
Q2
B — range() needs a number, not a string, so this causes a TypeError. A for loop can go straight over the characters of the message: for ch in text:.
Q3
B — The ciphertext is built by adding characters, so result must start as an empty string. Starting at 0 makes += chr(...) fail because you can't add a string to a number. It should be result = "".
Q4
B — == compares two values, it does not create a variable. result is never set, so result += ... causes a NameError. It should use a single = to assign: result = "".
Q5
C — chr() turns a number into a letter, but ch is a letter. To get its number code you need ord(ch). It should be n = ord(ch) - 65 + shift.