-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContact Book
90 lines (80 loc) · 2.96 KB
/
Contact Book
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
contacts = []
def add_contact():
name =input("Enter the name: ")
mobilenumber = input("Enter the mobile no: ")
emailid = input("Enter the emailis: ")
address = input("Enter the address: ")
contact = {'name':name, 'mobileno':mobilenumber, 'emailid':emailid, 'address':address}
contacts.append(contact)
print("Contact added successfully!")
def view_contact_list():
print("Contacts: ")
for idx, contact in enumerate(contacts, 1):
print(f"{idx}. {contact['name']}: {contact['mobileno']}")
def search_contact(query):
results = []
for contact in contacts:
if query.lower() in contact['name'].lower() or query in contact['mobileno']:
results.append(contact)
return results
def update_contact():
view_contact_list()
if contacts:
idx = int(input("Enter the index of the contact to update: "))
if 1 <= idx <= len(contacts):
contact = contacts[idx - 1]
print(f"Updating contact: {contact['name']}")
contact['name'] = input("Enter updated name: ")
contact['mobileno'] = input("Enter updated phone number: ")
contact['email'] = input("Enter updated email: ")
contact['address'] = input("Enter updated address: ")
print("Contact updated successfully!")
else:
print("Invalid index!")
else:
print("No contacts to update!")
def delete_contact():
view_contact_list()
if contacts:
idx = int(input("Enter the index of the contact to delete: "))
if 1 <= idx <= len(contacts):
contact = contacts.pop(idx - 1)
print(f"Contact '{contact['name']}' deleted successfully!")
else:
print("Invalid index!")
else:
print("No contacts to delete!")
def main():
while True:
print("\nMenu:")
print("1. Add Contact")
print("2. View Contact List")
print("3. Search Contact")
print("4. Update Contact")
print("5. Delete Contact")
print("6. Exit")
choice = input("Enter your choice: ")
if choice == '1':
add_contact()
elif choice == '2':
view_contact_list()
elif choice == '3':
query = input("Enter name or phone number to search: ")
results = search_contact(query)
if results:
print("Search results:")
for contact in results:
print(f"Name: {contact['name']}, Phone: {contact['mobileno']}")
else:
print("No matching contacts found.")
elif choice == '4':
update_contact()
elif choice == '5':
delete_contact()
elif choice == '6':
print("Exiting program...")
break
else:
print("Invalid choice!")
if __name__ == "__main__":
main()