Recap and Feedback
In this part, you defined classes and created instances with their own state. You used methods to keep that state valid and composition to build objects that work together. You also followed references to shared objects, made independent records for saving, and separated the interface, model, and storage into modules.
The central question is:
Which object is responsible for this state and the operations that change it?
Equipment Library
The concluding exercise brings together the equipment library developed in this part. Its command interface lets a user add items, borrow and return them, and save the library.
The exercise supplies the completed model and storage modules from earlier chapters, a small launcher, and a check file. Your task is to connect the model and file operations to the command interface in app.py. Work in your IDE so you can inspect saved records and test a complete quit-and-restart interaction. The Inline Editor remains available for checks within one execution.
Build and Check One Boundary at a Time
Run check_library.py once to see the supplied model and storage working together. These checks can pass before you implement the menu: they do not yet exercise app.main().
Build the interface in stages:
- Handle startup, including a missing or damaged save file, then support
quitand an unknown command. Check that damaged contents are not silently discarded. - Add one item and list it. Try an invalid addition and check that the list is unchanged.
- Borrow the same item twice, then return it twice. Compare each message with the method’s success flag and the next listing.
- Add saving. Test an empty library and a file error separately. Use disposable files. In your IDE, save, quit, and run the program again with the same path; check that the item and its availability are restored. Browser-generated files are temporary, so use a save/load check within one execution there.
Each stage should work before you add the next. The exercise awards 10 points for each stage, so you can submit working stages while later commands are still missing. The first three stages do not require saving; the final stage checks saving and complete interactions. The model already owns the item rules; the interface’s job is to call it and present the returned results accurately.
Here is one interaction to check once those stages work. Start with the supplied empty library.json.
| Action | Expected response |
|---|---|
| List the empty library | No items. |
Add E10, Camera, media | Item added. |
Borrow E10 | Item borrowed. |
Borrow E10 again | Cannot borrow item. |
| List the library | E10: Camera (media) - on loan |
| Save, then quit | Library saved., then Goodbye. |
Equipment library
0 / 40 points
Complete main() in app.py. The starter supplies the model and storage modules from the earlier exercises so you can focus on connecting their behavior to an interface. Keep those modules’ behavior. main.py is a supplied launcher; selecting either application file and Run starts one interaction. Importing any module must not start it.
Ask Library file: and call load_library(path). A FileNotFoundError starts a new EquipmentLibrary and prints Starting an empty library. Other OSError or ValueError failures print Could not load library. and end immediately. A damaged save must not become an empty library.
Then repeatedly ask Command: . Strip surrounding whitespace and lowercase only the command. Support:
| Command | Required behavior |
|---|---|
| add | Ask ID: , Name: , Category: ; pass their responses to add_item. Print Item added. on success or Item not added. on ValueError. |
| borrow | Ask ID: and strip it; use borrow_item. Print Item borrowed. for True, otherwise Cannot borrow item.. |
| return | Ask ID: and strip it; use return_item. Print Item returned. for True, otherwise Cannot return item.. |
| list | Use to_records and print each record in insertion order as id: name (category) - status, with status available or on loan. For no items print No items.. |
| save | Call save_library with the startup path and current library. Print Library saved. after success or Could not save library. on OSError. Continue in either case. |
| quit | Print Goodbye. and end without saving automatically. |
| anything else | Print Unknown command. and ask again. |
Every shown prompt has one trailing space. Identifiers are case-sensitive. The model handles trimming and validation for additions and rejects duplicate identifiers. Keep returned results meaningful: a rejected borrow must not print success.
Build and submit in four stages. Each completed stage earns 10 of the exercise’s 40 points:
| Stage | What to complete |
|---|---|
| 1 | Startup, including missing or damaged files; guarded imports and launchers; quit and unknown commands. |
| 2 | add and list, including invalid additions, insertion order, and current item status. |
| 3 | borrow and return, including repeated or missing-item attempts and messages that use the returned flags. |
| 4 | save, recovery from file errors, and complete interactions that preserve records and availability across restarts. |
The first three stages do not require a working save command. The final stage also checks the commands working together. You can submit a working stage before implementing later commands; use the feedback to choose what to complete next.
The supplied library.json is a valid empty library. Run check_library.py for the supplied model and file checks. These checks do not call your menu; test that separately with the command sequences. To try the menu in the course editor, run main.py and enter library.json at startup. Saving and loading checks must happen within one execution there; use downloaded files in a local practice folder to retain changes across runs. Missing parent folders need not be created.
Work in your IDE
An IDE is recommended for this exercise. Download it with the IDE plugin or choose Download starter code in the File Submission tab. Extract the files into a separate exercise folder and open that folder in your IDE, using the environment from Part 5.
From that folder, run py check_library.py on Windows or python3 check_library.py on macOS/Linux; use python instead if that is your configured command. The supplied checks cover model and storage behavior. Run main.py separately to test your menu, then save, quit, and restart with the same library path to check the restored items and availability. Extend the sample checks with cases from the contract.
Submit with the IDE plugin or upload a ZIP in the File Submission tab. Include the supplied source and sample files and your checks at the archive’s top level. Leave out generated saves, temporary test data, .venv, and __pycache__.
You can also use the Inline Editor tab. Open Show file tree, select check_library.py, and Run. Run executes the selected file; Submit sends the editor files for grading. Generated files are temporary, so any browser save/load check must happen within one execution.
Review the decisions your interface makes after the model or storage operation returns or raises.
Check Your Understanding
- How does a class differ from an instance, and what state belongs to each equipment item?
- Which checks establish valid item state, and which methods preserve it?
- How does a returned success flag determine what the interface prints?
- How does a reference to a live item differ from an independent record of its state?
- Which responsibilities belong to the interface, the model, and the storage module?
Feedback
Complete the feedback form below, including an estimate of the time you spent on this part. In the optional improvement field, you can describe a concept that was difficult, a design decision you changed after testing, or how AI assistance affected your understanding.
Reflection and feedback
0 / 5 points
Consider the following statements and answer them based on your experiences from studying the materials and working on the assignments in this part. When answering the numeric statements, answer them so that value 1 means that you strongly disagree with the statement, and value 7 means that you strongly agree with the statement. Please be as honest as possible when answering. The feedback will be used to improve the course.
Looking Ahead
You can now combine control flow, functions, collections, files, and classes in a complete application. In Part 7, you will create a small web application and practise requests, pages, and forms. A final example reads records from a database and displays them in a browser. Familiar Python functions and collections will connect these new interfaces to the programming you have already practised.