-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContactList.py
98 lines (83 loc) · 3.35 KB
/
ContactList.py
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
94
95
96
97
98
contacts = []
def addContact():
name = input("Enter name: ")
phone = input("Enter phone number: ")
while len(phone)!=10 or phone.isnumeric()==False:
print("Phone munbers must consist of 10 numbers")
phone = input("Enter phone number: ")
contact = {
'name': name,
'phone': phone,
}
contacts.append(contact)
print("Contact added successfully!")
def viewContact():
if not contacts:
print("No contacts available.")
return
print("List of Contacts:")
for index, contact in enumerate(contacts):
print(f"{index + 1}. Name: {contact['name']}, Phone: {contact['phone']}")
def searchContact():
searchTerm = input("Enter name or phone number to search: ")
foundContact = []
for contact in contacts:
if searchTerm.lower() in (contact['name']).lower() or searchTerm in contact['phone']:
foundContact.append(contact)
if foundContact:
print("Search Results:")
for contact in foundContact:
print(f"Name: {contact['name']}, Phone: {contact['phone']}")
else:
print("No matching contacts found.")
return foundContact
def updateContact():
foundContact = searchContact()
if foundContact:
searchTerm = input("Enter exact name or phone number to be upated from the searched items: ")
for contact in foundContact:
if searchTerm.lower() in (contact['name']).lower() or searchTerm in contact['phone']:
contact['name'] = input("Enter new name (press enter to keep current): ") or contact['name']
newPhone = input("Enter new phone number (press enter to keep current): ") or contact['phone']
while len(newPhone)!=10 or newPhone.isnumeric()==False:
print("Phone number must consist of 10 digits")
newPhone = input("Enter phone number: ")
contact['phone'] = newPhone
print("Contact updated successfully!")
return
print("Enter valid name or phone number!")
def deleteContact():
foundContact = searchContact()
if foundContact:
searchTerm = input("Enter exact name or phone number to be deleted: ")
for contact in foundContact:
if searchTerm.lower() in (contact['name']).lower() or searchTerm in contact['phone']:
contacts.remove(contact)
print("Contact deleted successfully!")
return
print("Enter valid name")
#main function
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 your choice (1-6): ")
if choice == '1':
addContact()
elif choice == '2':
viewContact()
elif choice == '3':
searchContact()
elif choice == '4':
updateContact()
elif choice == '5':
deleteContact()
elif choice == '6':
print("Exiting Contacts!")
break
else:
print("Invalid choice! Please enter a number between 1 and 6.")