Academy Computer Science Practical programming

Course strand · Python · ERL · Section B

Practical programming

Exam-shaped Python, OCR reference language, trace tables, and Section B tactics. Katas and a quiz — nothing locked, no required order.

Everything on this course is open. Skip, jump, retry. Nothing is locked and there is no required order.

Not a third exam paper. OCR still expects real coding hours. Paper 2 will punish anyone who has only read about loops.

Lewis already ships games. This page is exam-shaped Python and Exam Reference Language — the boring cousin of tyneside.games. Jump to any kata.

Python — the three constructs

Type these. Do not only read them.

Sequence

print("Tyneside")
print("Academy")

Selection

age = int(input("Age? "))
if age >= 16:
    print("GCSE sitting is plausible")
elif age >= 13:
    print("Early sitting needs a plan")
else:
    print("Plenty of time")

Iteration — count

for n in range(1, 6):
    print(n * n)

Iteration — condition

secret = "tyne"
guess = ""
while guess != secret:
    guess = input("Password: ")
print("In")

Nesting — a times table is the classic:

for row in range(1, 5):
    line = ""
    for col in range(1, 5):
        line = line + str(row * col) + "\t"
    print(line)

If you can trace that nested loop on paper, Section B gets easier.

Python — strings, lists, files, functions

Strings: s[0], s[-1], s[2:5], len(s), s.lower(), s.split(), s.replace("a", "o").

Lists (1D arrays):

scores = [8, 3, 9]
scores.append(7)
print(scores[0], len(scores), sum(scores))

2D:

board = [
    [".", ".", "."],
    [".", "X", "."],
    [".", ".", "O"],
]
print(board[1][1])  # X

Function:

def clamp(n, lo, hi):
    if n < lo:
        return lo
    if n > hi:
        return hi
    return n

File (read all lines):

with open("highscores.txt", "r") as f:
    for line in f:
        print(line.strip())

with closes the file even if you error — nicer than GCSE ERL, still worth using.

OCR Exam Reference Language

Paper 2 is allowed in Python or ERL. Learn both so a question never looks foreign.

Common ERL shapes:

x = int(input("x"))
if x MOD 2 == 0 then
  print("even")
else
  print("odd")
endif

for i = 0 to 4
  print(i)
next i

while x > 0
  x = x - 1
endwhile

array names[2]
names[0] = "Lewis"
print(names[0])

function double(n)
  return n * 2
endfunction

MOD is %. DIV is //. == for compare. = for assign. then / endif / next i are the visual difference from Python.

Translate one way then the other: Python → ERL → Python. That is the exercise.

Trace tables and dry-runs

Every Section B has a “what is the output” or “complete the table”.

Rules:

  1. One column per variable (and output)
  2. New row when any of them change
  3. Conditions get a True/False column if they control a loop
  4. Do not skip the last failing while check
a = 1
b = 1
for _ in range(5):
    c = a + b
    a = b
    b = c
print(a, b)

That is Fibonacci-ish. Trace it. Final print is the point.

Paper 2 Section B scenarios

A typical paper: a story (library, game, shop), some data stored in arrays, then:

  • Finish a procedure
  • Fix a logic error
  • Write a validation loop
  • Compute a total / max / count
  • Maybe a 2D array or a file

Method:

  1. Underline inputs, processes, outputs
  2. Name the arrays and their indexes as the paper does — do not invent a different structure
  3. Write ERL or Python consistently
  4. Trace your own answer once with their sample data
  5. Validation and sensible names are cheap marks

There is no time gate on this site. In the real hall you have 90 minutes for the whole of Paper 2. When you want pressure, set a kitchen timer yourself — the page will not do it to you.


Katas below. Pick any. Reveal a model. Then change the model (different limit, 2D, file) so it is yours.

Practice · optional · answers on this page

Exercises

Do as many or as few as you like, in any order. Hints and a model answer sit under each task.

py-core · e1 python

Guess the number

Computer picks a random integer 1–20. User guesses until correct. After each guess print "low", "high", or "got it" and the number of tries.
Hint
random.randint, while, a counter.
Show a model answer
import random secret = random.randint(1, 20) tries = 0 guess = 0 while guess != secret: guess = int(input("Guess 1-20: ")) tries = tries + 1 if guess < secret: print("low") elif guess > secret: print("high") else: print("got it in", tries)
py-extra · e2 python

High-score file

File scores.txt, one integer per line. Print how many scores, the highest, and the mean. If the file is missing, print a message and stop cleanly.
Hint
try/except FileNotFoundError. Strip empty lines.
Show a model answer
try: f = open("scores.txt", "r") except FileNotFoundError: print("No scores.txt") else: nums = [] for line in f: line = line.strip() if line != "": nums.append(int(line)) f.close() if len(nums) == 0: print("Empty file") else: best = nums[0] total = 0 for n in nums: total = total + n if n > best: best = n print(len(nums), best, total / len(nums))
py-extra · e3 python

2D battleship peek

5×5 grid of "~". Place two "S" ships at coordinates you choose in code. Ask the user for a row and col (0–4). Print HIT or MISS. Do not sink logic unless you want to — this is targeting practice.
Hint
Nested lists. Validate range (that's 2.3 leaking in — good).
Show a model answer
grid = [["~"] * 5 for _ in range(5)] grid[1][2] = "S" grid[4][4] = "S" row = int(input("row 0-4: ")) col = int(input("col 0-4: ")) if row < 0 or row > 4 or col < 0 or col > 4: print("off the map") elif grid[row][col] == "S": print("HIT") else: print("MISS")
erl · e4 paper

Translate both ways

1. Rewrite this Python in ERL: total = 0 for n in range(1, 6): total = total + n print(total) 2. Rewrite this ERL in Python: if x MOD 2 == 0 then print("even") else print("odd") endif
Hint
for n = 1 to 5 / next n. % instead of MOD.
Show a model answer
1. total = 0 for n = 1 to 5 total = total + n next n print(total) 2. if x % 2 == 0: print("even") else: print("odd")
trace · e5 paper

Trace this

x = 5 y = 1 while x > 0 y = y * x x = x - 1 endwhile print(y) Table: x, y, (x>0). What prints?
Hint
Factorial of 5.
Show a model answer
Start x=5 y=1, condition true y=5, x=4 y=20, x=3 y=60, x=2 y=120, x=1 y=120, x=0, condition false Prints 120
section-b · e6 paper

Mini Section B

A club stores member ages in an array ages[] of 6 integers. Write (Python or ERL): 1. Count how many are 18 or over. 2. A function is_adult(n) that returns true if n >= 18. 3. One validation: reject an age of -1 when inputting a replacement for ages[0].
Hint
Count with a for. Function returns Boolean. Loop until valid.
Show a model answer
count = 0 for i = 0 to 5 if ages[i] >= 18 then count = count + 1 endif next i function is_adult(n) if n >= 18 then return true else return false endif endfunction ages[0] = int(input("age")) while ages[0] < 0 ages[0] = int(input("age")) endwhile

Check yourself · not a gateway

Quiz

Mark it, reveal it, or skip it. A low score does not close anything. Try again as often as you want.

1 py-core range(1, 6) in Python produces which integers?
2 erl In OCR ERL, remainder after division is written…
3 trace In a while-loop trace table you should also record…
4 section-b Sensible first steps on a Section B scenario include… (Select all that apply)
5 py-extra board[0][0] is the bottom-right cell of a 5×5 grid stored as row-major lists.
6 py-core A while loop is the better construct when…