Python Project: A Contact Book
A contact book is a small program that stores names and phone numbers. This one stores its contacts in one file and shows its menu from another.
The storage file
contacts_store.py holds the list and the add function. The main program adds one contact and prints the list.
with open("contacts_store.py", "w") as f:
f.write("contacts = []\n\ndef add(name, phone):\n contacts.append({'name': name, 'phone': phone})\n\ndef all_contacts():\n return contacts\n")
import contacts_store
contacts_store.add("Ada", "555-0101")
print(contacts_store.all_contacts())Find one contact
find walks the list and returns the matching dict. A missing name gives back None.
with open("contacts_store.py", "w") as f:
f.write("contacts = []\n\ndef add(name, phone):\n contacts.append({'name': name, 'phone': phone})\n\ndef find(name):\n for c in contacts:\n if c['name'] == name:\n return c\n return None\n")
import contacts_store
contacts_store.add("Ada", "555-0101")
contacts_store.add("Bob", "555-0102")
print(contacts_store.find("Bob"))The menu in the main file
The main file prints the menu lines. The contacts themselves come from the storage file.
with open("contacts_store.py", "w") as f:
f.write("contacts = []\n\ndef add(name, phone):\n contacts.append({'name': name, 'phone': phone})\n\ndef all_contacts():\n return contacts\n")
import contacts_store
contacts_store.add("Ada", "555-0101")
contacts_store.add("Bob", "555-0102")
print("Contact Book")
print("1. Add a contact")
print("2. List contacts")
for c in contacts_store.all_contacts():
print(c["name"], c["phone"])Example
with open("contacts_store.py", "w") as f:
f.write("contacts = []\n\ndef add(name, phone):\n contacts.append({'name': name, 'phone': phone})\n\ndef find(name):\n for c in contacts:\n if c['name'] == name:\n return c\n return None\n\ndef all_contacts():\n return contacts\n")
import contacts_store
contacts_store.add("Ada", "555-0101")
contacts_store.add("Bob", "555-0102")
contacts_store.add("Cy", "555-0103")
print("All contacts:")
for c in contacts_store.all_contacts():
print(c["name"], c["phone"])
found = contacts_store.find("Bob")
print("Found:", found["name"], found["phone"])
print("Total:", len(contacts_store.all_contacts()))Key points
- Contact book: a program that stores names with phone numbers
- Put the contact list and its add, find, and list functions in the storage file
- Put the menu and the printouts in the main file
- The main file imports the storage file and calls its functions
- Use a list of dicts to hold the contacts, with name and phone in each dict
- Use a loop to search the list for one name
In this example, the program writes contacts_store.py first, with the list and three functions. The main program adds three contacts through the storage file. It then lists them all, finds Bob by name, and prints the total. The storage file never prints anything. The main file never touches the list directly.
Note
The storage file holds no printouts. The main file holds no contact data.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
Where does the contact list live in this two-file program?