Due 12/9 at 11:59pm
A contact book is a simple database where you store information about people. In this project, you will write a program that manages contacts using a dictionary data structure.
Your program must meet the following specifications:
For example:
Contact Book Menu
1. Add contact
2. View all contacts
3. Search for contact
4. Delete contact
5. Quit
Choice: 1
Enter name: Alice
Enter phone: 555-1234
Enter email: [email protected]
Enter address: 123 Main St
Contact saved!
Choice: 2
=== All Contacts ===
Alice - Phone: 555-1234, Email: [email protected], Address: 123 Main St
Choice: 3
Enter name to search: Alice
Found: Alice
Phone: 555-1234
Email: [email protected]
Address: 123 Main St
Choice: 5
Goodbye!
You can create a dictionary and add key-value pairs:
contacts = {}
contacts["Alice"] = {"phone": "555-1234", "email": "[email protected]", "address": "123 Main St"}
You can access values by key:
print(contacts["Alice"]["phone"]) # 555-1234
You can check if a key exists:
if "Alice" in contacts:
print("Contact found")
You can loop through a dictionary:
for name in contacts:
info = contacts[name]
print(f"{name}: {info['phone']}")
You can delete a key-value pair:
del contacts["Alice"]
def add_contact(contacts):
# TODO: Get name and details, add to dictionary, print confirmation
pass
def view_all_contacts(contacts):
# TODO: Loop through contacts and print each one, handle empty book
pass
def search_contact(contacts):
# TODO: Search by name, print info if found or error if not
pass
def delete_contact(contacts):
# TODO: Delete by name, print confirmation or error
pass
# Main program
contacts = {}
while True:
print("\n=== Contact Book Menu ===")
print("1. Add contact")
print("2. View all contacts")
print("3. Search for contact")
print("4. Delete contact")
print("5. Quit")
choice = input("\nChoice: ")
if choice == "1":
add_contact(contacts)
elif choice == "2":
view_all_contacts(contacts)
elif choice == "3":
search_contact(contacts)
elif choice == "4":
delete_contact(contacts)
elif choice == "5":
print("Goodbye!")
break
else:
print("Invalid choice. Try again.")