Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

def menu():

contactList = {} #CONTACTS
while True:
#Menu
print()
print("Welcome to My Phone Directory:")
print("Number of contacts in your directory:", len(contactList))
print("These are the functions available:\n"
"1. Add new contact \n"
"2. Search for contact\n"
"3. Display all contact details in directory \n"
"4. Update existing contact details \n"
"5. Remove existing contact \n"
"0. Exit program")
print()
selection = int(input("Your selection please:"))

if len(contactList) == 0:
if selection == 2 or selection == 3 or selection == 4 or
selection == 5:
print("That selection is not valid as your contact list is
empty")
print()
else:
if selection == 0:
break
elif selection == 1:
addContact(contactList)
else:
if selection == 0:
break
elif selection == 1:
addContact(contactList)
elif selection == 2:
searchContact(contactList)
elif selection == 3:
displayContact(contactList)
elif selection == 4:
updateContact(contactList)
elif selection == 5:
removeContact(contactList)
#ADD CONTACTS
def addContact(contactList):
print("Add Contact:")
contact = {} #STORE DETAILS
hobbies = [] #STORE HOBBY DETAILS
contact_name = input("Please enter the contact name: ")
contact_address = input("Please enter contact's address: ")
contact_mobile = int(input("Please enter the contact's mobile
number:"))
while True:
print()
contact_hobby = input("Please enter hobby(Enter 'end' to stop
adding): ")
if contact_hobby == "end":
break
else:
hobbies.append(contact_hobby)

contactList[contact_name] = contact
contact["address"] = contact_address
contact["number"] = contact_mobile
contact["hobbies"] = hobbies
print("Contact added! \n")
#SEARCH FOR CONTACTS
def searchContact(contactList):
while True:
print()
choice = int(input("Search Contact: \n"
"Search By:\n"
"1. Name \n"
"2. Contact number \n"
"3. Hobbies \n"
"0. Return to main menu\n"
"Your choice:"))
print()
#NAME SEARCH
if choice == 1:
contact_search = input("Please enter the name you wish to
search by: ")
if contact_search in contactList:
print("\n"
"Contact details:")
print("Name:", contact_search)
print("Address:", contactList[contact_search]['address'])
print("Mobile number:",
contactList[contact_search]['number'])
print("Hobbies:", contactList[contact_search]['hobbies'])
else:
print("Contact not found!")
print()
#SEARCH MOBILE NUMBER
elif choice == 2:
contact_search = int(input("Please enter the contact you wish
to search by: "))
count = 0
for i in contactList:
temp = contactList[i].get('number', "Contact not found!")
if temp == contact_search:
print("\n"
"Contact details:")
print("Contact details:")
print("Name:", i)
print("Address:", contactList[i]['address'])
print("Mobile number:", contact_search)
print("Hobbies:", contactList[i]['hobbies'])
count += 1
else:
continue
if count == 0:
print("Contact not found!")
print()
#SEARCH HOBBIES
elif choice == 3:
searchhobby = input("Please enter the hobby you wish to search
by: ")
count = 0
for i in contactList:
if searchhobby in contactList[i]['hobbies']:
print("\n"
"Contact details:")
print("Name:", i)
print("Address:", contactList[i]['address'])
print("Mobile number:", contactList[i]['number'])
print("Hobbies:", contactList[i]['hobbies'])
count = count + 1
else:
continue
if count == 0:
print("Contact not found!")
print()
elif choice == 0:
break

#DISPLAY CONTACTS
def displayContact(contactList):
print("Display all contacts:")
for i in contactList:
print("\n"
"Contact details:")
print("Name:", i)
print("Address:", contactList[i]['address'])
print("Mobile number:", contactList[i]['number'])
print("Hobbies:", contactList[i]['hobbies'])
print("\n"
"\n")

#UPDATE CONTACT DETAILS


def updateContact(contactList):
print("Update Contact")
contact_update = input("Please enter the name of the contact you would
like to update: ")
if contact_update in contactList:
print("\n"
"Contact details:")
print("Name:", contact_update)
print("Address:", contactList[contact_update]['address'])
print("Mobile number:", contactList[contact_update]['number'])
print("Hobbies:", contactList[contact_update]['hobbies'])
while True:
print("\n"
"Contact Exist \n"
"1. Address \n"
"2. Contact no \n"
"3. Hobbies \n"
"0. Exit")
choice = int(input("What would you like to update?"))
if choice == 1:#ADDRESS UPDATE
updated_address = input("Enter the new address: ")
contactList[contact_update]['address'] = updated_address
print("Address Updated!")
elif choice == 2:#NUMBER UPDATE
updated_number = input("Enter the new mobile number: ")
contactList[contact_update]['number'] = updated_number
print("Mobile Number Updated!")
elif choice == 3: #HOBBIES UPDATE
contactList[contact_update]['hobbies'].clear()
while True:
print()
updated_hobby = input("Please enter hobby (Enter 'end'
to stop adding): ")
if updated_hobby == "end":
print("Hobbies updated")
break
else:

contactList[contact_update]["hobbies"].append(updated_hobby)
elif choice == 0: #RETURN
break
else:
print("No such contact found.")
# CONTACT REMOVAL
def removeContact(contactList):
contact_remove = input("Please enter the contact name you want to
remove: ")
if contact_remove in contactList:
print("\n"
"Contact details:")
print("Name:", contact_remove)
print("Address:", contactList[contact_remove]['address'])
print("Mobile number:", contactList[contact_remove]['number'])
print("Hobbies:", contactList[contact_remove]['hobbies'])
confirmation = input("Is this the contact you want to remove?
(Yes/No) :")
#accept both yes for error prevention
if confirmation == "No" or confirmation == "no":
print("Contact was not removed!")
# accept both no for error prevention
elif confirmation == "Yes" or confirmation == "yes":
del contactList[contact_remove]
print("Contact removed!")
else:
print("No such contact found.")
menu()

You might also like