0% found this document useful (0 votes)
4 views

13

The document contains a Python script for a contact management system that allows users to add, view, search, update, and delete contacts stored in a JSON file. It includes functions for loading and saving contacts, as well as a main loop for user interaction. The script handles various user inputs and provides feedback on actions taken.

Uploaded by

Harsh Durgude
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

13

The document contains a Python script for a contact management system that allows users to add, view, search, update, and delete contacts stored in a JSON file. It includes functions for loading and saving contacts, as well as a main loop for user interaction. The script handles various user inputs and provides feedback on actions taken.

Uploaded by

Harsh Durgude
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Code:

import json

def load_contacts():

try:

with open("contacts.json", "r") as f:

return json.load(f)

except FileNotFoundError:

return []

def save_contacts(contacts):

with open("contacts.json", "w") as f:

json.dump(contacts, f, indent=4)

def add_contact(contacts):

name = input("Enter name: ")

phone = input("Enter phone: ")

email = input("Enter email: ")

address = input("Enter address: ")

contacts.append({"name": name, "phone": phone, "email": email, "address": address})

print("Contact added.")

def view_contacts(contacts):

if not contacts:

print("No contacts found.")

return

for contact in contacts:

print(f"Name: {contact['name']}, Phone: {contact['phone']}, Email: {contact['email']}, Address:


{contact['address']}")

def search_contact(contacts, search_term):

results = [c for c in contacts if search_term.lower() in c['name'].lower() or search_term in c['phone']]

if not results:

print("No matching contacts found.")

return

for contact in results:

print(f"Name: {contact['name']}, Phone: {contact['phone']}, Email: {contact['email']}, Address:


{contact['address']}")

def update_contact(contacts, search_term):

for contact in contacts:

if search_term.lower() in contact['name'].lower() or search_term in contact['phone']:

print("Enter new information:")

contact['name'] = input(f"New name ({contact['name']}): ") or contact['name']

contact['phone'] = input(f"New phone ({contact['phone']}): ") or contact['phone']

contact['email'] = input(f"New email ({contact['email']}): ") or contact['email']

contact['address'] = input(f"New address ({contact['address']}): ") or contact['address']


print("Contact updated.")

return

print("Contact not found.")

def delete_contact(contacts, search_term):

for contact in contacts:

if search_term.lower() in contact['name'].lower() or search_term in contact['phone']:

contacts.remove(contact)

print("Contact deleted.")

return

print("Contact not found.")

def main():

contacts = load_contacts()

while True:

print("\nContact Manager")

print("1. Add Contact")

print("2. View Contacts")

print("3. Search Contact")

print("4. Update Contact")

print("5. Delete Contact")

print("6. Exit")

choice = input("Enter choice: ")

if choice == "1":

add_contact(contacts)

elif choice == "2":

view_contacts(contacts)

elif choice == "3":

search_contact(contacts, input("Enter search term: "))

elif choice == "4":

update_contact(contacts, input("Enter name or phone to update: "))

elif choice == "5":

delete_contact(contacts, input("Enter name or phone to delete: "))

elif choice == "6":

save_contacts(contacts)

break

else:

print("Invalid choice. Please try again.")

if __name__ == "__main__":

main()

Output:

Contact Manager
1. Add Contact

2. View Contacts

3. Search Contact

4. Update Contact

5. Delete Contact

6. Exit

Enter choice: 2

Name: Harsh, Phone: 1234567890, Email: [email protected], Address:123 Main St, sangamner

Name: Pranav, Phone: 9876543210, Email: [email protected], Address:456 Elm St, nashik

Contact Manager

1. Add Contact

2. View Contacts

3. Search Contact

4. Update Contact

5. Delete Contact

6. Exit

Enter choice: 1

Enter name: shivam

Enter phone: 1234567890

Enter email: [email protected]

Enter address: sangamner

Contact added.

Contact Manager

1. Add Contact

2. View Contacts

3. Search Contact

4. Update Contact

5. Delete Contact

6. Exit

Enter choice:6

You might also like