Edexcel GCSE Computer Science June 2024 Paper 2, Question 2: the Caesar cipher

Pearson Edexcel 1CP2/02 June 2024, Question 2: choose the correct lines to make a Caesar cipher work, shifting capital and lower case letters with wrap-around in both directions and leaving other characters alone. Each choice explained, with the cipher to run.

Past paper questionPearson 1CP2/02June 2024 Paper 210 marksComplete the program

Question 2 of the Pearson Edexcel GCSE Computer Science Paper 2 sat on 21 May 2024 (1CP2/02, Application of Computational Thinking) is worth 10 marks. In ten places the file offers several commented-out lines, and you uncomment the right one. To choose, you have to understand how the whole cipher works.

We do not copy the exam paper or Pearson's code files here. Open the paper beside this page: Edexcel June 2024 Paper 2 question paper (PDF). When you have finished, check the mark scheme too.

The question in short

A Caesar cipher shifts each letter a set number of places along the alphabet. A positive shift goes right and a negative shift goes left. Going past Z carries on from A, and going back past A carries on from Z. Spaces, symbols and digits are not changed.

The paper's test data: "The Rainbow" with a shift of 4 gives "Xli Vemrfsa". "Alphabet Soup" with -5 gives "Vgkcvwzo Njpk". "123 ^& Bye" with 9 gives "123 ^& Khn".

How it works

For each character:

  1. Is it a letter? letter.isalpha(). If not, add it to the ciphertext unchanged.
  2. Turn it into its ASCII code with ord() and add the shift.
  3. Is it a capital? letter.isupper(). If the new code is past ord('Z'), take 26 off. If it is before ord('A'), add 26.
  4. Otherwise is it lower case? letter.islower(). The same two checks, against ord('z') and ord('a').
  5. Turn the code back into a character with chr() and add it to the ciphertext.

The ten choices

The line must The right choice
test for a letter if (letter.isalpha ()):
test for a capital if (letter.isupper ()):
catch a capital shifted past the end if (value > ord ('Z')):
catch a capital shifted before the start elif (value < ord ('A')):
test for lower case elif (letter.islower ()):
catch lower case past the end if (value > ord ('z')):
catch lower case before the start elif (value < ord ('a')):
make the new letter newLetter = chr (value)
add an encrypted letter cipherText = cipherText + newLetter
add an unchanged character cipherText = cipherText + letter

Where the marks are lost

  • Uncommenting two lines in one group. The mark for that group is lost, even if one of them is right.
  • >= against ord('Z'). Z itself is a valid result. Only codes past Z wrap round.
  • Mixing up ord and chr. ord goes from a character to a number. chr comes back.
  • Adding letter where newLetter is wanted. The output would be the plaintext.
  • Not running the test data. All three rows, including the negative shift and the symbols.

Run it

The whole cipher, written out. Type a message and a shift.

The Rainbow with 4 gives Xli Vemrfsa. Alphabet Soup with -5 gives Vgkcvwzo Njpk. Encrypt something, then decrypt it with the opposite shift.
The program
from bugbot import *
connect()

plainText = input("Message: ")
shift = int(input("Shift: "))
cipherText = ""

for letter in plainText:
    if letter.isalpha():
        value = ord(letter) + shift
        if letter.isupper():
            if value > ord("Z"):
                value = value - 26
            elif value < ord("A"):
                value = value + 26
        elif letter.islower():
            if value > ord("z"):
                value = value - 26
            elif value < ord("a"):
                value = value + 26
        newLetter = chr(value)
        cipherText = cipherText + newLetter
    else:
        cipherText = cipherText + letter

print(cipherText)
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Questions

How does a Caesar cipher work?

Each letter of the message is replaced by the letter a fixed number of places further along the alphabet, wrapping round from Z to A. To decrypt, shift the same number of places the other way.

How do I shift a letter in Python?

Turn it into its code with ord(), add the shift, correct it by 26 if it has gone past the end or the start of the alphabet, and turn it back with chr().

What do isalpha(), isupper() and islower() do?

They are string methods that return True or False. isalpha() is True for letters, isupper() for capital letters and islower() for lower case letters.

More from this paper

Every Edexcel 1CP2 question we have worked

Learn it step by step

  1. F3.2 Character codes and conversion Strings, lists and records
  2. F11.6 Encryption Cyber security
  3. F3.1 String handling Strings, lists and records
Open the lessons

This is our own explanation of a published exam question. It is not written or endorsed by Pearson, and the question paper and mark scheme remain Pearson's copyright. Read them on Pearson's site with the links on this page.