Course 4.2Lesson 1 of 5
A contact book has four jobs: add a contact, find a number, remove a contact and list everybody, and a dictionary is the right tool because we look things up by name. Two rules make it safe: the key is always the name in small letters so that Ravi and ravi are one contact, and a number must be digits only, which add enforces by raising a ValueError. Each job is a small function with a note that says what it does, and the main program catches the error so that it does not stop. We plan the cases and the expected book by hand, test the edges, and extend the book with a search by the start of a name.
Try the code and the simulations beside the lesson.
After this lesson you can
A contact book must do four jobs: add a contact, find a number, remove a contact and list everybody. A dictionary is the right tool, because we look things up by name.
Two rules make it safe:
Ravi, ravi and RAVI are the same contact.The book starts with Asha (98765) and Ravi (91234). Six commands are run. Work out the answers by hand.
| Command | What should happen |
|---|---|
| add Meera 99887 | added |
| add Kabir 12ab | refused: the number has a letter |
| find Ravi | 91234 |
| find Zoya | not in the book |
| remove Asha | removed |
| remove Asha again | nobody called Asha |
At the end the book holds Meera and Ravi, in alphabetical order.
Predict first, then run it. Compare with your table.
| Test | Expected |
|---|---|
a name in capitals, RAVI | finds Ravi |
a number with spaces, 98 765 | refused (a space is not a digit) |
| an empty number | refused ("".isdigit() is False) |
| adding a name that exists | the number is replaced |
| find in an empty book | None |
| remove from an empty book | False |
The fourth row is a choice: replace silently, or refuse? A real book might ask. Decide, and write the rule in a comment.
The same idea makes the search kind to the user: look up name.lower(), and any spelling finds the contact.
A book is more useful if it can find a name from its first letters. Two small functions: all the names that start with some letters, and all the contacts whose number starts with some digits.
Pick one or two, and do them without looking at a solution. Write the expected answers first.
count() gives the number of contacts.rename(old, new) moves a number to a new name, and refuses if the new name exists.Ravi and ravi become two contacts.contacts[name] for a missing name. A KeyError.except: in the main loop. A typing mistake would be hidden.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
add, find and remove, and the checker tries them.lower()), so that small differences do not make two contacts.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
name.lower() for the key?contacts.get("zoya") give if there is no Zoya?remove check with in before del?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.