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

23. The Club Library · Give Each Book a Record

Use named dictionary keys to read and update related information.

Lesson files · lessons/lists-and-records/dictionary-records

Before you start
- Store text and True or False under names.
- Read a list item with square brackets.

By the end
- Use a dictionary to store several details about one book.
- Use a key to add and then read the room field.
- Explain when get() returns its fallback value.

Where you can use it
Store a borrowed game’s name, shelf and availability together. Read a detail by its key and update the shelf when the game moves.

A dictionary connects a key to each value. In the example the key "title" points to the text "Sky Lab", and the key "available" points to True. Use book["title"] to get the title by its key.

When room might be missing, book.get("room", "Main") returns Main as a fallback. To store a room, use an assignment such as book["room"] = "Media Room". After the assignment, the record has a room key you can read.

Predict
Why does the second line say Main?
- room is missing, so get uses its default.
- Every dictionary has a room key.

Worked example (example.py)

book = {"title": "Sky Lab", "available": True}
print(book["title"])
print(book.get("room", "Main"))


Example result
Sky Lab
Main

Read a room name and store it in the book record. Print the title and then the room.

Starter program (starter.py)

book = {"title": "Sky Lab", "available": True}
print(book["title"])
print(book.get("room", "Main"))


Hint
Use book["room"] = input() after creating the dictionary.

Case 1. Try the sample
Input
Media Room
Expected output
Sky Lab
Media Room

Case 2. Move the book
Input
Café Corner
Expected output
Sky Lab
Café Corner

Stretch
Add a player-friendly label that shows whether the book can be borrowed.

Reflect
When is a missing key with a default useful?

Check your prediction
room is missing, so get uses its default. get returns the supplied default for a missing key.
