The project involves creating a basic contact management system.
This project will cover various list
operations such as adding, removing, and searching for contacts, as well as sorting and displaying the
contact list.
Mini Project: Contact Management System
Objectives
- Understand how to use Python lists for data storage.
- Practice basic list operations: add, remove, search, sort, and display.
- Implement a simple menu-driven program.
Project Outline
1. Display Menu: Show options to the user.
2. Add Contact: Add a new contact to the list.
3. Remove Contact: Remove a contact from the list.
4. Search Contact: Search for a contact by name.
5. **Display All Contacts: Show all contacts in the list.
6. **Sort Contacts**: Sort the contact list by name.
Step-by-Step Implementation
Step 1: Display Menu
Create a function to display a menu with options for the user to choose from.
def display_menu():
print("\nContact Management System")
print("1. Add Contact")
print("2. Remove Contact")
print("3. Search Contact")
print("4. Display All Contacts")
print("5. Sort Contacts")
print("6. Exit")
Step 2: Add Contact
Create a function to add a new contact to the list.
def add_contact(contacts):
name = input("Enter contact name: ")
phone = input("Enter contact phone number: ")
contacts.append({"name": name, "phone": phone})
print("Contact added successfully!")
Step 3: Remove Contact
Create a function to remove a contact by name.
def remove_contact(contacts):
name = input("Enter contact name to remove: ")
for contact in contacts:
if contact["name"] == name:
contacts.remove(contact)
print("Contact removed successfully!")
return
print("Contact not found!")
Step 4: Search Contact
Create a function to search for a contact by name.
def search_contact(contacts):
name = input("Enter contact name to search: ")
for contact in contacts:
if contact["name"] == name:
print("Contact found:", contact)
return
print("Contact not found!")
Step 5: Display All Contacts
Create a function to display all contacts in the list.
def display_contacts(contacts):
if not contacts:
print("No contacts to display!")
else:
for contact in contacts:
print("Name:", contact["name"], ", Phone:", contact["phone"])
Step 6: Sort Contacts
Create a function to sort contacts by name.
def sort_contacts(contacts):
contacts.sort(key=lambda x: x["name"])
print("Contacts sorted successfully!")
Step 7: Main Program
Combine all the functions into a menu-driven program.
def main():
contacts = []
while True:
display_menu()
choice = input("Enter your choice: ")
if choice == "1":
add_contact(contacts)
elif choice == "2":
remove_contact(contacts)
elif choice == "3":
search_contact(contacts)
elif choice == "4":
display_contacts(contacts)
elif choice == "5":
sort_contacts(contacts)
elif choice == "6":
print("Exiting program...")
break
else:
print("Invalid choice! Please try again.")
if __name__ == "__main__":
main()
Additional Features (Optional)
To make the project more advanced, you can add the following features:
- Save contacts to a file and load them when the program starts.
- Validate phone numbers.
- Implement editing of existing contacts.
- Add an option to export contacts to a CSV file.
This project provides a practical way to learn about Python lists and basic file handling while building a
useful application.