──────────────────────────────────────

41. Build Your Own Python Program · Turn a Request into Test Cases

Turn someone’s request into inputs, a decision rule and expected outputs.

Lesson files · lessons/build-your-own-program/write-testable-examples

Before you start
- Read two numbers with input() and int().
- Use an if condition to compare numbers.

By the end
- Write one input and expected result for each side of a decision.
- Run a case at the exact limit and explain why it gives that result.

Where you can use it
Agree on what a booking, supply or scoring tool should do before you start writing it.

Ask what information a person can give you and what answer will help them. A club can tell you how many places are free and how many they need. Your rule is precise when you can say what happens for fewer, exactly enough and too many places.

Write a tiny table on paper with input, expected result and actual result. Fill in the expected column first. Compare against it after running the program. A boundary case is a value right at the point where the decision changes.

Predict
What happens when four pencils are needed and exactly four are available?
- The result is True.
- The result is False.

Worked example (example.py)

def enough_pencils(needed, available):
    return available >= needed

print(enough_pencils(4, 4))
print(enough_pencils(5, 4))


Example result
True
False

Build a two-line seat check. Read seats available and seats requested, in that order. Print Booked when there are enough seats. Print Waiting list when the group needs more seats than are available.

Starter program (starter.py)

available = int(input())
requested = int(input())
print("Waiting list")


Starter input
4
4

Hint
Write out the results for four available seats and requests for three, four and five seats.

Case 1. Try the sample
Input
4
4
Expected output
Booked

Case 2. Seats left over
Input
6
3
Expected output
Booked

Case 3. One seat short
Input
4
5
Expected output
Waiting list

Stretch
Try one free seat and one person. Invent another short test for a full event.

Reflect
What result would change if the rule said there must be a spare seat?

Check your prediction
The result is True. The greater-than-or-equal comparison includes equality.
