Show7-Pro

Concepts Covered:

The current image has no alternative text. The file name is: Show.jpg
  • File handling (open, read, write)
  • JSON module (json.dump, json.load)
  • Lists and dictionaries
  • Functions and loops
  • Data persistence

๐ŸŽฏ Objective:

Create a simple, text-based contact book application that allows users to:

  • Add new contacts
  • View all saved contacts
  • Search by name
  • Edit existing contacts
  • Delete contacts
  • Automatically save and load data using a JSON file

๐Ÿ’ก Code (contact_book.py):

import json
import os

# File to store contacts
CONTACT_FILE = "contacts.json"

# Load contacts if the file exists
def load_contacts():
    if os.path.exists(CONTACT_FILE):
        with open(CONTACT_FILE, "r") as f:
            return json.load(f)
    return []

# Save contacts to the JSON file
def save_contacts(contacts):
    with open(CONTACT_FILE, "w") as f:
        json.dump(contacts, f, indent=4)

# Display all contacts
def view_contacts(contacts):
    if not contacts:
        print("\n๐Ÿ“ญ No contacts found.\n")
        return
    print("\n๐Ÿ“’ Your Contacts:")
    for i, contact in enumerate(contacts, start=1):
        print(f"{i}. {contact['name']} - {contact['phone']} - {contact['email']}")
    print()

# Add a new contact
def add_contact(contacts):
    name = input("๐Ÿ‘ค Enter name: ")
    phone = input("๐Ÿ“ž Enter phone number: ")
    email = input("๐Ÿ“ง Enter email: ")

    contacts.append({"name": name, "phone": phone, "email": email})
    save_contacts(contacts)
    print(f"โœ… Contact '{name}' added successfully!\n")

# Search for a contact
def search_contact(contacts):
    search_name = input("๐Ÿ” Enter name to search: ").lower()
    found = [c for c in contacts if search_name in c['name'].lower()]
    if found:
        print("\n๐Ÿ”Ž Search Results:")
        for c in found:
            print(f"๐Ÿ‘ค {c['name']} | ๐Ÿ“ž {c['phone']} | ๐Ÿ“ง {c['email']}")
    else:
        print("\nโŒ No contacts found with that name.\n")

# Edit an existing contact
def edit_contact(contacts):
    name = input("โœ๏ธ Enter the name of the contact to edit: ").lower()
    for contact in contacts:
        if contact['name'].lower() == name:
            print(f"\nEditing contact: {contact['name']}")
            new_name = input("๐Ÿ‘ค New name (leave blank to keep current): ") or contact['name']
            new_phone = input("๐Ÿ“ž New phone (leave blank to keep current): ") or contact['phone']
            new_email = input("๐Ÿ“ง New email (leave blank to keep current): ") or contact['email']

            contact.update({"name": new_name, "phone": new_phone, "email": new_email})
            save_contacts(contacts)
            print(f"โœ… Contact '{new_name}' updated successfully!\n")
            return
    print("โš ๏ธ Contact not found.\n")

# Delete a contact
def delete_contact(contacts):
    name = input("๐Ÿ—‘๏ธ Enter name of contact to delete: ").lower()
    updated_contacts = [c for c in contacts if c['name'].lower() != name]

    if len(updated_contacts) < len(contacts):
        save_contacts(updated_contacts)
        print(f"โœ… Contact '{name}' deleted successfully!\n")
    else:
        print("โš ๏ธ Contact not found.\n")

    return updated_contacts

# Main program
def main():
    contacts = load_contacts()
    while True:
        print("===== ๐Ÿ“˜ CONTACT BOOK =====")
        print("1๏ธโƒฃ View Contacts")
        print("2๏ธโƒฃ Add Contact")
        print("3๏ธโƒฃ Search Contact")
        print("4๏ธโƒฃ Edit Contact")
        print("5๏ธโƒฃ Delete Contact")
        print("6๏ธโƒฃ Quit")

        choice = input("๐Ÿ‘‰ Enter your choice: ")

        if choice == "1":
            view_contacts(contacts)
        elif choice == "2":
            add_contact(contacts)
        elif choice == "3":
            search_contact(contacts)
        elif choice == "4":
            edit_contact(contacts)
        elif choice == "5":
            contacts = delete_contact(contacts)
        elif choice == "6":
            print("๐Ÿ‘‹ Exiting Contact Book. Goodbye!")
            break
        else:
            print("โš ๏ธ Invalid choice. Try again!\n")

if __name__ == "__main__":
    main()

๐Ÿง  Example Usage

python contact_book.py

Console Output Example:

===== ๐Ÿ“˜ CONTACT BOOK =====
1๏ธโƒฃ View Contacts
2๏ธโƒฃ Add Contact
3๏ธโƒฃ Search Contact
4๏ธโƒฃ Edit Contact
5๏ธโƒฃ Delete Contact
6๏ธโƒฃ Quit
๐Ÿ‘‰ Enter your choice: 2
๐Ÿ‘ค Enter name: Alice
๐Ÿ“ž Enter phone number: 123456789
๐Ÿ“ง Enter email: alice@example.com
โœ… Contact 'Alice' added successfully!

๐Ÿ’พ Notes
  • All contacts are saved automatically in contacts.json
  • You can open the file anytime to view or back up your data
  • The edit feature allows keeping existing fields if you leave them blank
โฌ‡๏ธ Download Show7-Pro ุชุญู…ูŠู„ Server 1

Related Posts

FlyFeditv

๐Ÿ“˜ Introduction In this project, youโ€™ll create a command-line Expense Tracker that allows users to: โœ… Add expenses with category, amount, and descriptionโœ… View total spending and category breakdownโœ… Automatically…

Read more

NetCinFly

๐Ÿ“˜ Introduction In this project, youโ€™ll develop a Student Grades Analyzer in Python that can: This project helps you strengthen your skills with lists, dictionaries, loops, and data analysis logic….

Read more

Eagle_Pro

๐ŸŒ Introduction In this project, youโ€™ll create a Web Scraper App in Python that extracts quotes, authors, and tags from a live website.Youโ€™ll use the BeautifulSoup and Requests libraries to…

Read more

Cobra_Pro

๐ŸŒฆ๏ธ Introduction In this project, youโ€™ll build a Weather App in Python that retrieves live weather information from an online API.Youโ€™ll learn how to work with HTTP requests, JSON data,…

Read more

Rapid_tv

๐Ÿ”น Introduction The Rock, Paper, Scissors Game is a classic hand game that you can easily build in Python. ๐Ÿ‘‰ Rules: ๐Ÿ”น Code Example ๐Ÿ”น Example Run ๐Ÿ”น Concepts Learned…

Read more

VOD-ZalHD

๐Ÿ”น Introduction The Number Guessing Game is a classic beginner Python project. ๐Ÿ”น Code Example ๐Ÿ”น Example Run ๐Ÿ”น Concepts Learned ๐Ÿ”น Summary โฌ‡๏ธ Download VOD-ZalHD ุชุญู…ูŠู„ Server 1 โฌ‡๏ธ…

Read more