Paul's Python CoursePPC

Project 9 of 13, for Riya

Contact Book

Riya's clients and suppliers: look people up, find them from half a name, and add new ones from one typed line.

About 70 minutes, in 17 steps, free and with no account needed.

Start the project

No account needed. Your work is kept in this browser until you sign up.

The brief

  • RiyaI'm drawing for half the class now, posters and covers and portraits.
  • RiyaTheir numbers are all over the place, in a group chat, a sketchbook, even on the back of a print.
  • RiyaPlus the print shop and framer every week. I want to type a name, even half of one, and get the number.
  • PaulNames to details? That sounds like it wants a dictionary.

contacts holds a record for each name: a dictionary with a phone and an address. Ask Name? until a blank line.

  1. A name in the book gets its record: Paul: 01632 960101, The Workshop.
  2. add asks New contact? for a whole contact on one line, like Aaesha, 01632 960733, The Engine Room. Split it at each , , add the record, and print Added Aaesha. A line that doesn't split into exactly three parts gets Name, phone, address, please.
  3. Any other name gets No contact called print. Then, if what was typed is one of the words in any contact's name, ignoring capitals, print Try: and those names, in book order, with , between them.
What it prints
Name? Paul
Paul: 01632 960101, The Workshop
Name? shop
No contact called shop.
Try: Print Shop, Frame Shop
Name? add
New contact? Aaesha, 01632 960733, The Engine Room
Added Aaesha.
Name? Aaesha
Aaesha: 01632 960733, The Engine Room
Name?

What you learn

The lesson, step by step

  1. Names to details

    A dictionary pairs keys with values, so you can look a value up by its key. Here the keys are names and the values are rooms.

    It's how Riya's book will work: type a name, get the details. A list finds things by position. A dictionary finds them by key.

    You write it in curly brackets, with a colon between each key and its value. rooms["Yahya"] looks up Yahya. rooms["Neil"] = "The Puzzle Room" adds Neil. len(rooms) counts the keys. The example prints The Archive, the whole dictionary, then 3.

    The usual slip is to expect a key to hold two values. Each key appears once. Give an existing key a new value, and the old one is replaced.

  2. A new room

    Riya is noting where everyone can be found. Line 2 changes her mind about Neil.

  3. A name that might not be there

    Looking up a key that isn't there, like rooms["Riya"], stops the program with a KeyError. .get() asks more carefully.

    Riya's book will be asked for names it doesn't have yet, and it mustn't crash when it is.

    rooms.get("Riya", "Not in the book") gives Riya's value if she's a key, and "Not in the book" if she isn't. Leave out the second value and a missing key gives None, Python's word for no value at all. "Paul" in rooms asks whether Paul is a key: True or False.

    The example prints Not in the book, The Archive, then True.

    The usual slip is to expect rooms["Riya"] to give a blank. It crashes. Add print(rooms["Riya"]) at the end and run it to see.

  4. A name that isn't there

    Riya's book will be asked for names that aren't in it yet. Find out what happens.

  5. Every name in the book

    A for loop over a dictionary gives you its keys, one per pass, in the order they went in.

    That's how Riya's book will search: go through every name and check each one.

    To get the values too, loop over rooms.items(). It hands you each key and its value together, so for name, room in rooms.items(): puts the key in name and the value in room. The example prints the three names, then a line for each, like Paul is in The Workshop.

    The usual slip is to expect the plain loop to give the values. It gives the keys. Inside it, rooms[name] looks up each value.

  6. Going through the book

    Riya keeps count of who has ordered prints.

  7. A record for each name

    A dictionary's values can be dictionaries too. Each contact in Riya's book will be a small dictionary of its own, a record, with a phone and an address.

    That way one name holds several details, and every record has the same keys.

    Read book["Neil"]["address"] from the left: book["Neil"] is Neil's record, and ["address"] looks inside it. The example prints The Puzzle Room, then Paul: 01632 960101.

    Inside an f-string written with double quotes, put the key in single quotes, record['phone']. Another double quote would end the text early.

    The usual slip is to look for book["address"]. address is a key of each record, not of the book.

  8. Which key

    Riya's book, with one record in it so far.

  9. Cutting a line into pieces

    .split() cuts a string into a list of pieces. line.split(", ") cuts at every comma and space, and throws those away.

    New contacts will arrive as one typed line, and the program needs the name, phone and address apart.

    With nothing in the brackets, .split() cuts at the spaces, which turns a name into its words. Then in asks whether a word is one of the items: "Shop" in words is True. The example prints the three parts, the first part, the two words, then True.

    Methods can follow one another. "Print Shop".lower().split() lowers the name, then splits what that gives: ['print', 'shop'].

    The usual slip is line.split(",") without the space. It leaves a space at the start of every piece after the first.

  10. The third piece

    A contact for Neil, typed on one line.

  11. Gluing pieces together

    .join() does the opposite of .split(). It glues a list of strings into one string, with a separator between each pair.

    Riya's search will find a list of names, and she wants them on one line: Try: Print Shop, Frame Shop.

    Write the separator first, then .join() with the list in the brackets: ", ".join(names). The example prints Aaesha / 01632 960733 / The Engine Room, then Try: Print Shop, Frame Shop.

    The usual slip is to write it the way round you'd say it, names.join(", "). Python stops with an error, because a list has no join. It belongs to the separator.

  12. One line, three parts

    Aaesha's contact, typed on one line the way Riya will type new ones.

  13. How Paul would do it

    • PaulI've got the clock's spare parts in drawers for when I finally use them, and nobody remembers their full names.

    Paul's spare parts for the clock are a dictionary of records, like Riya's book. Each part's record has a drawer and a count of how many are left.

    The loop stops on "", empty text, which is what input() gives when you press Enter without typing. He checks part in drawers before looking a part up, so a part he doesn't have never crashes it. For a part that isn't there, he searches the words of every name and joins what he finds onto one line.

    The labels beside the code are the steps he took. Riya's book takes the same steps, plus one for add.

  14. Fix Riya's version

    • RiyaI made a quick phone list before the proper book, and every name I tried worked.
    • RiyaThen Aaesha typed Yahya and it said KeyError. Can you have a look? Something's off.

    Run Riya's lookup and type Yahya, then a blank line. It should say:

    No number for Yahya.
    

    Fix it so a name that isn't a key in phones doesn't crash it.

  15. Make the contact book

    Build Riya's contact book. The brief has the whole spec.

    Name? print
    No contact called print.
    Try: Print Shop
    Name? add
    New contact? Aaesha, 01632 960733, The Engine Room
    Added Aaesha.
    Name?
    

    The comments in the file are Paul's labels from the parts drawers, plus one for add. That one splits the typed line at ", ", as in the Try it on .split(). Check for add before you look the name up. Nobody in the class is called Add, and Riya would like to keep it that way.

    The checks look people up, add contacts, search with part of a name in any capitals, and try add lines with too few or too many parts. A word only counts if it's a whole word of a name: op finds nothing.

  16. Ship it

    • RiyaPrint Shop, then Frame Shop, both of them in the order they went in.
    • RiyaWho's going to use it? Me, every time one of my prints needs a frame.
    • AaeshaThere's one record for everyone, and with no copies there's nothing to go out of date.