Dictionaries and Other Collections · capstone
Capstone project: Project: Build a Searchable Mini Catalog
You now have the pieces for a small structured information system:
for records with labeled fields; a
for several records in order; for processing every record; for focused operations; a search function that returns a dictionary or
None;input for a real query.
Your project combines them into a searchable mini catalog.
The catalog records
The starter contains three books. Keep their data and shape:
catalog = [
{
"title": "Kindred",
"creator": "Octavia E. Butler",
"year": 1979,
},
{
"title": "A Wizard of Earthsea",
"creator": "Ursula K. Le Guin",
"year": 1968,
},
{
"title": "Python Crash Course",
"creator": "Eric Matthes",
"year": 2023,
},
]
Every record has the same required keys. You may add more records after the project passes, but keep these three so the prepared search can be graded.
format_item(item)
Return one line in this exact form:
Kindred by Octavia E. Butler (1979)
The function receives one dictionary and returns a
show_catalog(catalog)
Print the heading:
CATALOG
Then print every record as a numbered line starting at 1:
1. Kindred by Octavia E. Butler (1979)
Call format_item(item) for the text after the number, and keep a counter beside the loop for the number itself, the way you have since Chapter 7.
find_by_title(catalog, query)
Use the contract from “Search a List of Records”:
clean the query with
.strip().lower();loop through the records;
compare with each lowercase title;
return the matching dictionary;
return
Noneafter the loop if no title matches.
The top-level program
After the function definitions and catalog:
call
show_catalog(catalog);ask exactly
Search by title:;call
find_by_title;print
No matching title.if the result isNone;otherwise print
Found:followed byformat_item(match).
Submit supplies this query:
PYTHON CRASH COURSE
Your cleaning should make it find the third record without changing the stored title.
Build in three passes
1. Format one record
Complete format_item. Call it temporarily with catalog[0] and verify the line.
2. Show all records
Complete show_catalog. Check the numbering, record order, and exact punctuation.
3. Search
Complete find_by_title, then connect the input and result branch. Try an existing title with strange capitalization, then a missing title.
Read the final structure
The top-level program should be short. It says to show, ask, search, and report. The functions hold the details, the list holds the order, and each dictionary gives one record meaningful fields.
That is the central outcome of this chapter: choosing structures that make the information and the operations agree.
Chapter 10 turns to the failures that real input and imperfect data can cause. You will learn to read
Task
Build the searchable mini catalog described in the lesson.
Requirements:
Complete
format_item(item)so it returnstitle by creator (year)from one. Complete
show_catalog(catalog). PrintCATALOG, then every record as a numbered line starting at1. Callformat_itemfor each record.Complete
find_by_title(catalog, query). Clean the query, return the matching dictionary, or returnNoneafter the. Keep the three starter records and their exact fields.
After the definitions and data, show the catalog.
Ask exactly
Search by title:.Print
No matching title.forNone, orFound:followed by the formatted matching record.
Submit uses prepared input, so you do not need to type during grading.