Lesson 19: Capstone I — Contact book
First capstone! We'll build a small contact book that combines two things we learned: dictionaries (Module 1) and error handling (Module 5). The name is the key, the phone number is the value — we chose a dict over a list because we look up by name, which is exactly a dict's strength: a direct looku
A contact book is a dictionary: name → number. To avoid crashing when you look up a name that isn't there, guard with try/except or use .get() with a default.
- contact book as a dict
- A dict where the name is the key and the number is the value: contacts["Dana"].
- guarding KeyError
- Accessing a missing key with [] raises KeyError; wrap it in try/except to handle it gracefully.
- .get() with a default
- .get(key, default) returns the value if the key exists, otherwise the default — without crashing.