-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact_book.py
More file actions
93 lines (72 loc) · 2.49 KB
/
Copy pathcontact_book.py
File metadata and controls
93 lines (72 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import json
FILE_NAME = "contacts.json"
# Load contacts from file
try:
with open(FILE_NAME, "r") as file:
contacts = json.load(file)
except:
contacts = {}
while True:
print("\n===== CONTACT BOOK =====")
print("1. Add Contact")
print("2. View Contacts")
print("3. Search Contact")
print("4. Update Contact")
print("5. Delete Contact")
print("6. Save Contacts")
print("7. Exit")
choice = input("Enter your choice: ")
if choice == "1":
name = input("Enter Name: ")
phone = input("Enter Phone Number: ")
email = input("Enter Email: ")
contacts[name] = {
"Phone": phone,
"Email": email
}
print("Contact Added Successfully!")
elif choice == "2":
if contacts:
print("\n--- Contact List ---")
for name, details in contacts.items():
print(f"\nName: {name}")
print(f"Phone: {details['Phone']}")
print(f"Email: {details['Email']}")
else:
print("No Contacts Found!")
elif choice == "3":
name = input("Enter Name to Search: ")
if name in contacts:
print("\nContact Found:")
print(f"Phone: {contacts[name]['Phone']}")
print(f"Email: {contacts[name]['Email']}")
else:
print("Contact Not Found!")
elif choice == "4":
name = input("Enter Name to Update: ")
if name in contacts:
new_phone = input("Enter New Phone Number: ")
new_email = input("Enter New Email: ")
contacts[name]["Phone"] = new_phone
contacts[name]["Email"] = new_email
print("Contact Updated Successfully!")
else:
print("Contact Not Found!")
elif choice == "5":
name = input("Enter Name to Delete: ")
if name in contacts:
del contacts[name]
print("Contact Deleted Successfully!")
else:
print("Contact Not Found!")
elif choice == "6":
with open(FILE_NAME, "w") as file:
json.dump(contacts, file, indent=4)
print("Contacts Saved Successfully!")
elif choice == "7":
with open(FILE_NAME, "w") as file:
json.dump(contacts, file, indent=4)
print("Contacts Saved. Exiting Program...")
break
else:
print("Invalid Choice! Please Try Again.")