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

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

Delux_pro

šŸ”¹ Introduction The To-Do List App is one of the most popular beginner projects. It teaches you how to: šŸ”¹ Code Example šŸ”¹ Features of This App šŸ”¹ Example Run…

Read more

Fam4k

šŸ”° Project Overview A calculator is one of the simplest yet most effective projects to start with in Python.You will learn: āœļø Step 1: Plan the Calculator We need: šŸ–„ļø…

Read more

Speed_HD+

1. Introduction Data visualization helps us understand patterns and insights in data by converting raw numbers into charts and graphs.In Python, the most popular libraries are: 2. Installing Required Libraries…

Read more

Dauo

1. Introduction Web scraping is the process of extracting data from websites. In Python, we commonly use libraries like requests (to fetch web pages) and BeautifulSoup (to parse and extract…

Read more