Edexcel International Advanced Level Computer Science, point 19.1.3
The seven features are described in 9.1.3. In Unit 4 you must use them together to organise a larger program. The specification points to the PLS and the Good Programming Practice Guide (GPPG).
The features
| Feature | Meaning | Example |
|---|---|---|
| a. Blocked code | Indented groups of statements that belong together | A loop body, an if branch |
| b. User-defined subprograms | Subprograms you write | def total_with_vat(prices) |
| c. Built-in subprograms | Provided by the language | len, sum, sorted, open |
| d. Libraries of subprograms | Collections of ready-made code you import |
math, random, json |
| e. Files | Your own modules, in separate .py files |
import inventory |
| f. Procedures | Subprograms that do an action and return no value | show_menu() |
| g. Functions | Subprograms that return a value | find_item(code) |
A small multi-file project: a stock list
Layout:
stock/
|- data.py storage: load and save (files, JSON)
|- logic.py processing: add, find, low stock (functions)
|- ui.py input and output (procedures)
'- main.py controls the program
data.py (storage, using a library and a file)
import json
def load(filename):
try:
with open(filename) as f:
return json.load(f)
except FileNotFoundError:
return {}
def save(stock, filename):
with open(filename, "w") as f:
json.dump(stock, f, indent=2)
logic.py (functions only: no input or output)
LOW_LEVEL = 5
def add_item(stock, code, quantity):
stock[code] = stock.get(code, 0) + quantity
return stock
def find_item(stock, code):
return stock.get(code)
def low_stock(stock, level=LOW_LEVEL):
return sorted(code for code, qty in stock.items() if qty < level)
ui.py (procedures: show things, ask things)
def show_menu():
print("1. Add 2. Find 3. Low stock 4. Quit")
def ask(prompt):
return input(prompt).strip()
def show_low(codes):
print("Low stock:", ", ".join(codes) if codes else "none")
main.py (controls the flow)
import data
import logic
import ui
FILE = "stock.json"
def main():
# a function from our module
stock = data.load(FILE)
# blocked code: the loop body
while True:
ui.show_menu()
choice = ui.ask("Choice: ")
if choice == "1":
code = ui.ask("Code: ")
qty = int(ui.ask("Quantity: "))
logic.add_item(stock, code, qty)
elif choice == "2":
print(logic.find_item(stock, ui.ask("Code: ")))
elif choice == "3":
ui.show_low(logic.low_stock(stock))
elif choice == "4":
break
data.save(stock, FILE)
if __name__ == "__main__":
main()
Where each feature appears:
| Feature | Example above |
|---|---|
| Blocked code | The while and if/elif blocks |
| User-defined subprograms | add_item, find_item, show_menu |
| Built-in subprograms | sorted, input, print, open, int |
| Libraries | import json |
| Files (our own modules) | data, logic, ui |
| Procedures | show_menu, show_low (no return value) |
| Functions | find_item, low_stock, load (return values) |
Testing a module alone
Because logic.py has no input or output, it can be tested directly:
stock = {}
logic.add_item(stock, "A1", 3)
logic.add_item(stock, "A1", 2)
logic.add_item(stock, "B2", 10)
assert logic.find_item(stock, "A1") == 5
assert logic.find_item(stock, "ZZ") is None
assert logic.low_stock(stock) == [] # 5 is not below 5
logic.add_item(stock, "C3", 1)
assert logic.low_stock(stock) == ["C3"]
Design guidance
- One job per module (storage, logic, interface).
- Modules should depend on each other as little as possible (
logicdoesn't importui). - Use
if __name__ == "__main__":so importing a module doesn't run the program. - Pass data through parameters, not shared globals.
- Use built-ins and libraries before writing your own.
- Choose clear names for modules and subprograms.
Worked example
Task: split this single-file program into modules, naming the file for each part.
import random
prices = {"tea": 2.5, "cake": 3.0}
def total(order): return sum(prices[i] for i in order)
def ask(): return input("Item: ")
def show(t): print("Total", t)
Redesign:
| File | Contents | Feature |
|---|---|---|
menu.py |
prices dictionary |
Data module |
calc.py |
total(order) |
Function, no input or output |
ui.py |
ask() (a function that returns text), show(t) (a procedure) |
Input and output |
main.py |
imports the others and runs the order | Controls the program |
import random is unused and would be removed. Result: calc.total can be tested with a made-up price list, and the interface can change without touching the calculation.
Exam tip: know each feature and give an example. Say whether a subprogram is a function (returns a value) or a procedure (doesn't). Keep input and output out of logic modules. Use import correctly.
Check yourself
What is the difference between a library and one of your own files (modules)?
A library is ready-made code supplied by others (such as math); your own file is a module you write and import for your own subprograms.
What is the difference between a function and a procedure?
A function returns a value; a procedure performs an action and returns nothing.
Why shouldn't logic.py import ui.py?
Logic should be independent of the interface so that it can be tested alone and reused with a different interface.
What does if __name__ == "__main__": do?
It makes the code run only when the file is run directly, not when it is imported.