Project: Contact Book
Time to combine everything. You will build a tiny contact book where each contact is a dict and all of them live in one list. This list-of-dicts shape is the backbone of countless real programs.
contacts = [
{"name": "Ada", "phone": "111"},
{"name": "Sam", "phone": "222"},
]You will write three functions, the classic CRUD operations (create, read, delete) over that list:
add_contact(contacts, name, phone)appends a new dict and returns nothingfind_contact(contacts, name)returns the matching dict, orNoneif no one matchesdelete_contact(contacts, name)removes the matching contact and returnsTrue, or returnsFalseif there was nothing to remove
To find, loop over the contacts and compare each contact["name"] to the name you want. Return as soon as you find a match. To delete, find it first, then use contacts.remove(match).
Write three functions over a list of contact dicts. add_contact(contacts, name, phone) appends {"name": name, "phone": phone}. find_contact(contacts, name) returns the dict whose "name" matches, else None. delete_contact(contacts, name) removes the matching contact and returns True, or returns False when no match exists.
This lesson is locked
Lessons open one at a time. Finish the previous lesson to unlock this one.