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

24. The Club Library · Build a Club Library Report

Filter a list of records and handle an empty result.

Lesson files · lessons/lists-and-records/club-library

Before you start
- Read and update a value in a dictionary.
- Use a loop and append() to collect matching items.
- Count a list with len().

By the end
- Use a loop to check availability in each book record.
- Use the filtered list to print the count and titles.
- Test a catalogue with some available books and one with none.

Where you can use it
Make a list of sports gear the class can borrow today. Read each item’s availability, then show the number of choices and their names.

A catalogue is a list of book dictionaries. On each trip through the loop, book holds one dictionary. When the input is closed, set book["available"] to False before checking that book's availability.

Use a separate if to read the availability flag. Append the title when the flag is True. Count and print the selected titles after the loop so the count describes the entire result.

Predict
What value should go into the result list for each available book?
- The number of all books.
- Its title.

Worked example (example.py)

books = [{"title": "Sky Lab", "available": True}]
for book in books:
    print(book["title"])


Example result
Sky Lab

Use the starter catalogue. Input browse reports current availability. Input closed sets each book unavailable first. Print Available: and the count, then one line per available title in catalogue order.

Starter program (starter.py)

books = [
    {"title": "Sky Lab", "available": True},
    {"title": "Moon Garden", "available": False},
    {"title": "Star Map", "available": True},
]
mode = input().strip()
print(f"Available: {len(books)}")


Starter input
browse

Hint
Apply the closed update before filtering, then use if book["available"] for each record.

Case 1. Try the sample
Input
browse
Expected output
Available: 2
Sky Lab
Star Map

Case 2. Closed catalogue
Input
closed
Expected output
Available: 0

Project checklist
Run the sample case.

Project checklist
Try one different case and explain what happened.

Marking criterion
Program behaviour (4 points)
Each required behaviour works on the supplied cases.

Marking criterion
Use of the lesson concept (3 points)
The code uses the main idea in a clear, purposeful way.

Marking criterion
Test evidence (2 points)
The reflection describes a case, its expected result and what happened.

Marking criterion
Explanation (1 points)
The learner explains a change or design decision in their own words.

Stretch
Add a reserved flag and decide whether reserved books should appear.

Reflect
What evidence shows that the tool handles a catalogue with zero available books?

Check your prediction
Its title. A list of titles can be counted and printed without carrying the extra fields.
