-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact_book.py
More file actions
58 lines (52 loc) · 1.72 KB
/
Copy pathcontact_book.py
File metadata and controls
58 lines (52 loc) · 1.72 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
CONTACTS_FILE ="contacts.txt"
def add_contact():
name = input("Enter name: ")
phone = input("Enter phone number: ")
email = input("Enter email address: ")
with open(CONTACTS_FILE, "a") as file:
file.write(f"{name},{phone},{email}\n")
print("✅ Contact added!")
def view_contacts():
print("\n📒 Contact List:")
try:
with open(CONTACTS_FILE, "r") as file:
for line in file:
name, phone, email = line.strip().split(",")
print(f"Name: {name} | Phone: {phone} | Email: {email}")
except FileNotFoundError:
print("No contacts found.")
def search_contacts():
search_term = input("Search name: ").lower()
found = False
try:
with open(CONTACTS_FILE, "r") as file:
for line in file:
name, phone, email = line.strip().split(",")
if search_term in name.lower():
print(f"🔍 Found: {name} | {phone} | {email}")
found = True
if not found:
print("No matching contact found.")
except FileNotFoundError:
print("No contacts file found.")
def main():
while True:
print("\n📞 Contact Book Menu")
print("1. Add contact")
print("2. View contacts")
print("3. Search contacts")
print("4. Exit")
choice = input("Choose an option (1-4): ")
if choice == "1":
add_contact()
elif choice == "2":
view_contacts()
elif choice == "3":
search_contacts()
elif choice == "4":
print("Goodbye!")
break
else:
print("Invalid option. Please try again.")
if __name__ == "__main__":
main()