|
| 1 | +def add_contact(contacts): |
| 2 | + name = input("Enter contact name: ") |
| 3 | + phone = input("Enter contact phone number: ") |
| 4 | + contacts.append({"name": name, "phone": phone}) |
| 5 | + print(f"Contact '{name}' added successfully.\n") |
| 6 | + |
| 7 | +def view_contacts(contacts): |
| 8 | + if not contacts: |
| 9 | + print("No contacts available.\n") |
| 10 | + return |
| 11 | + print("Contacts List:") |
| 12 | + for idx, contact in enumerate(contacts, start=1): |
| 13 | + print(f"{idx}. Name: {contact['name']}, Phone: {contact['phone']}") |
| 14 | + print() # Add a new line for better readability |
| 15 | + |
| 16 | +def delete_contact(contacts): |
| 17 | + view_contacts(contacts) |
| 18 | + if not contacts: |
| 19 | + return |
| 20 | + try: |
| 21 | + index = int(input("Enter the number of the contact to delete: ")) - 1 |
| 22 | + if 0 <= index < len(contacts): |
| 23 | + removed_contact = contacts.pop(index) |
| 24 | + print(f"Contact '{removed_contact['name']}' deleted successfully.\n") |
| 25 | + else: |
| 26 | + print("Invalid contact number.\n") |
| 27 | + except ValueError: |
| 28 | + print("Please enter a valid number.\n") |
| 29 | + |
| 30 | +def main_menu(): |
| 31 | + contacts = [] |
| 32 | + |
| 33 | + while True: |
| 34 | + print("Contact Management System") |
| 35 | + print("1. Add Contact") |
| 36 | + print("2. View Contacts") |
| 37 | + print("3. Delete Contact") |
| 38 | + print("4. Exit") |
| 39 | + |
| 40 | + choice = input("Select an option (1-4): ") |
| 41 | + |
| 42 | + if choice == '1': |
| 43 | + add_contact(contacts) |
| 44 | + elif choice == '2': |
| 45 | + view_contacts(contacts) |
| 46 | + elif choice == '3': |
| 47 | + delete_contact(contacts) |
| 48 | + elif choice == '4': |
| 49 | + print("Exiting the program. Goodbye!") |
| 50 | + break |
| 51 | + else: |
| 52 | + print("Invalid choice. Please select a valid option.\n") |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + main_menu() |
0 commit comments