The answersDownload the PDF
Worksheet

F3.2 Character codes and conversion

Strings, lists and records · GCSE · OCR J277 2.2.2, AQA 8525 3.2.8, Edexcel 1CP2 6.3.3 · about 15 min

BugBotLab
NameClassDate

What this lesson is about

ord and chr, ASCII, comparing strings, and a Caesar cipher.

Questions 7 marks in all

  1. [1 mark]What does this program print?

    print(ord("A"), ord("a"))
  2. [1 mark]What does this program print?

    print(chr(66) + chr(111) + chr(116))
  3. [1 mark]ord("A") is 65. What is ord("E")?

  4. [1 mark]Why is "Zebra" < "apple" True?

    1. ACapital Z has a smaller character code than lower-case a
    2. BZebra is a shorter word
    3. CPython sorts words backwards
    4. DIt is False
  5. [1 mark]What does this program print?

    letter = "Y"
    code = (ord(letter) - ord("A") + 3) % 26
    print(chr(code + ord("A")))
  6. [1 mark]Which AQA pseudo-code function gives the code for a character?

    1. ACHAR_TO_CODE
    2. BASC
    3. Cord
    4. DSTRING_TO_INT
  7. [1 mark]What does this program print?

    print("42".isdigit(), "4two".isdigit())

The task: shift the letters

Ask Message? and print the message with every letter moved one place along the alphabet. The task answers HAL (the computer in a famous film), and the answer is the name of a famous computer company. The message will be capital letters only. Work it out with ord and chr.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

message = input("Message? ")
secret = ""

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/f3-2-character-codes-and-conversion/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Print the whole alphabet with one loop and chr, without typing any letters.
  2. Make the cipher keep spaces as they are, so that HELLO ROBOT works.
  3. Play a note for each letter of a name: tone(ord(letter) * 5, 0.2). Which name sounds best?