top of page

How to Build a Simple Contact Book in Python

Simple Contact Book
Simple Contact Book

Simple Contact Book. This project is a great next step because it introduces you to using dictionaries to store and manage structured data, a fundamental concept in programming.

First, here is the explanation of how the project is built.


How the "Simple Contact Book" is Made


  1. The Core Idea: The goal is to create a command-line program where you can add, view, and store contact information. This simulates a basic address book, but instead of saving to a file (an intermediate step), it will keep the contacts in memory while the program is running.

  2. The Data Structure: A Dictionary: A dictionary is the perfect tool for this job. We'll use a main dictionary (e.g., contacts) to hold our entire contact book. The keys of this dictionary will be the contact's name (a string), and the values will be another, nested dictionary containing that person's details, like their phone number and email. For example: {'Alice': {'phone': '123-456', 'email': 'alice@email.com'}}.

  3. Creating a Menu-Driven App: To make the program interactive, we need a main loop (while True) that continuously runs and presents the user with a menu of options:

    • Add a new contact

    • View all contacts

    • Exit the program

  4. Handling User's Choice: Inside the loop, we'll use input() to ask the user to choose an option from the menu. We then use if, elif, and else statements to determine which action to perform based on their choice.

  5. Organizing with Functions: To keep the code clean and readable, we'll break the logic into separate functions.

    • add_contact(contacts_dict): This function will ask the user for a name, phone, and email. It will then add this information as a new entry into the main contacts dictionary.

    • view_contacts(contacts_dict): This function will loop through the main contacts dictionary and print out each contact's name and their details in a nicely formatted way. It will also handle the case where the contact book is empty.

  6. Exiting the Program: If the user selects the "Exit" option from the menu, the break keyword is used to terminate the while loop, and the program ends with a goodbye message.



Comments


bottom of page