Strings, lists and records · GCSE · OCR J277 2.2.2, AQA 8525 3.2.8, Edexcel 1CP2 6.3.3 · about 15 min
ord and chr, ASCII, comparing strings, and a Caesar cipher.
[1 mark]What does this program print?
print(ord("A"), ord("a"))65 97
In ASCII, A is 65 and lower case starts 32 later, at 97.
[1 mark]What does this program print?
print(chr(66) + chr(111) + chr(116))
Bot
chr turns each code back into its character: B, o, t.
[1 mark]ord("A") is 65. What is ord("E")?
[1 mark]Why is "Zebra" < "apple" True?
[1 mark]What does this program print?
letter = "Y"
code = (ord(letter) - ord("A") + 3) % 26
print(chr(code + ord("A")))B
Y is letter 24; 24 + 3 is 27, and 27 % 26 is 1, which wraps round to B.
[1 mark]Which AQA pseudo-code function gives the code for a character?
CHAR_TO_CODEASCordSTRING_TO_INT[1 mark]What does this program print?
print("42".isdigit(), "4two".isdigit())True False
isdigit is True only when every character is a digit.
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 = ""The hint students can ask for: Turn each letter into its code number, move it along by one, and wrap back to the start of the alphabet when it runs past Z. Build the answer up one letter at a time.
from bugbot import *
connect()
message = input("Message? ")
secret = ""
for letter in message:
code = (ord(letter) - ord("A") + 1) % 26
secret = secret + chr(code + ord("A"))
print(secret)
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.