We have spent the last several posts exploring Python’s powerful dictionary data structure. Now it’s time to put all that theory into practice. In this mini-project, we will build a functional, interactive, command-line contact book.
Our program will be able to add new contacts, view a list of all contacts, look up the details for a specific contact, and exit. This project will make heavy use of nested dictionaries (from Post #70) to store our data cleanly and efficiently.
Step 1: The Data Structure and Main Loop
The core of our program will be a single dictionary called contacts
. The keys of this dictionary will be our contacts’ names (strings). The value for each name will be another dictionary containing that contact’s phone number and email.
Let’s create a new file called contacts.py
and set up the main application loop, just like we did in our previous projects.
# The main dictionary to store all contacts.
# Keys will be names, values will be another dictionary with details.
contacts = {}
print("--- Welcome to your Contact Book ---")
while True:
print("\n--- Contact Book Menu ---")
print("1. Add a new contact")
print("2. View all contacts")
print("3. Look up a contact")
print("4. Exit")
choice = input("Enter your choice (1-4): ")
# We will add the logic for our choices here
if choice == '4':
print("Goodbye!")
break
This gives us a running program that we can now add features to.
Step 2: Implementing the Menu Choices
Now, let’s build out the if-elif-else
chain to handle the user’s choices.
Choice 1: Add a Contact
If the user chooses “1”, we need to ask for the new contact’s name, phone, and email. We’ll then create a nested dictionary for the details and add it to our main contacts
dictionary.
if choice == '1':
name = input("Enter the contact's name: ")
phone = input("Enter the contact's phone number: ")
email = input("Enter the contact's email address: ")
contacts[name] = {"phone": phone, "email": email}
print(f"Contact for {name} added successfully!")
Choice 2: View All Contacts
If the user chooses “2”, we’ll loop through the contacts
dictionary using the .items()
method (from Post #67) and print out each contact’s details. We should also handle the case where the contact book is empty.
elif choice == '2':
print("\n--- All Contacts ---")
if not contacts:
print("Your contact book is empty.")
else:
for name, details in contacts.items():
print(f"Name: {name}")
print(f" Phone: {details['phone']}")
print(f" Email: {details['email']}")
Choice 3: Look Up a Contact
If the user chooses “3”, we’ll ask for a name and then check if that name exists as a key in our dictionary. If it does, we’ll print the details. If not, we’ll inform the user.
elif choice == '3':
name_to_find = input("Enter the name of the contact to look up: ")
if name_to_find in contacts:
details = contacts[name_to_find]
print(f"\n--- Details for {name_to_find} ---")
print(f"Phone: {details['phone']}")
print(f"Email: {details['email']}")
else:
print(f"Sorry, no contact found with the name '{name_to_find}'.")
The Complete Program
Here is the full, commented script for our contact book.
# The main dictionary to store all contacts.
contacts = {}
print("--- Welcome to your Contact Book ---")
while True:
print("\n--- Contact Book Menu ---")
print("1. Add a new contact")
print("2. View all contacts")
print("3. Look up a contact")
print("4. Exit")
choice = input("Enter your choice (1-4): ")
if choice == '1': # Add a contact
name = input("Enter the contact's name: ")
phone = input("Enter the contact's phone number: ")
email = input("Enter the contact's email address: ")
contacts[name] = {"phone": phone, "email": email}
print(f"Contact for '{name}' added successfully!")
elif choice == '2': # View all contacts
print("\n--- All Contacts ---")
if not contacts:
print("Your contact book is empty.")
else:
for name, details in contacts.items():
print(f"Name: {name}")
print(f" Phone: {details['phone']}")
print(f" Email: {details['email']}")
elif choice == '3': # Look up a contact
name_to_find = input("Enter the name of the contact to look up: ")
if name_to_find in contacts:
details = contacts[name_to_find]
print(f"\n--- Details for {name_to_find} ---")
print(f"Phone: {details['phone']}")
print(f"Email: {details['email']}")
else:
print(f"Sorry, no contact found with the name '{name_to_find}'.")
elif choice == '4': # Exit
print("Goodbye!")
break
else: # Handle invalid input
print("Invalid choice. Please enter a number from 1 to 4.")
What’s Next?
Excellent work! You’ve built a practical contact manager using a nested dictionary as its core data structure. This project solidifies everything we’ve learned about creating, accessing, modifying, and looping through dictionaries.
We’ve now covered lists, tuples, and dictionaries—three of Python’s four main built-in collection types. There is one more to explore, which is specialized for handling collections of unique items. In Post #75, we will be introduced to sets.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com