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.
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.
- A name in the book gets its record:
Paul: 01632 960101, The Workshop. addasksNew contact?for a whole contact on one line, likeAaesha, 01632 960733, The Engine Room. Split it at each,, add the record, and printAdded Aaesha.A line that doesn't split into exactly three parts getsName, phone, address, please.- 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, printTry:and those names, in book order, with,between them.
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
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 printsThe Archive, the whole dictionary, then3.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.
A new room
Riya is noting where everyone can be found. Line 2 changes her mind about Neil.
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 givesNone, Python's word for no value at all."Paul" in roomsasks whether Paul is a key:TrueorFalse.The example prints
Not in the book,The Archive, thenTrue.The usual slip is to expect
rooms["Riya"]to give a blank. It crashes. Addprint(rooms["Riya"])at the end and run it to see.A name that isn't there
Riya's book will be asked for names that aren't in it yet. Find out what happens.
Every name in the book
A
forloop 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, sofor name, room in rooms.items():puts the key innameand the value inroom. The example prints the three names, then a line for each, likePaul 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.Going through the book
Riya keeps count of who has ordered prints.
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
phoneand anaddress.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 printsThe Puzzle Room, thenPaul: 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"].addressis a key of each record, not of the book.Which key
Riya's book, with one record in it so far.
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. Theninasks whether a word is one of the items:"Shop" in wordsisTrue. The example prints the three parts, the first part, the two words, thenTrue.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.The third piece
A contact for Neil, typed on one line.
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 printsAaesha / 01632 960733 / The Engine Room, thenTry: 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 nojoin. It belongs to the separator.One line, three parts
Aaesha's contact, typed on one line the way Riya will type new ones.
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
drawerand a count of how many areleft.The loop stops on
"", empty text, which is whatinput()gives when you press Enter without typing. He checkspart in drawersbefore 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.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
phonesdoesn't crash it.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 foraddbefore 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
addlines with too few or too many parts. A word only counts if it's a whole word of a name:opfinds nothing.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.