From b6585ed0722ac7e576c5c656cc6c02773239f9d7 Mon Sep 17 00:00:00 2001 From: weston-bailey Date: Fri, 8 Dec 2023 10:05:09 -0800 Subject: [PATCH] finishes deliverable --- python/src/contacts.py | 32 +++++++++++++++++++++++++++++--- python/src/main.py | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/python/src/contacts.py b/python/src/contacts.py index 4dce478..bf73310 100644 --- a/python/src/contacts.py +++ b/python/src/contacts.py @@ -1,8 +1,34 @@ def show_contacts(addressbook): - pass + print() + for contact in addressbook: + print(f"{contact['name']} ({contact['email']}): {contact['phone']}") + print() def add_contact(addressbook): - pass + name = input("Enter Name: ").strip() + phone = input("Enter phone: ").strip() + email = input("Enter email: ").strip() + + new_contact = { + "name": name, + "phone": phone, + "email": email + } + + addressbook.append(new_contact) + + print(f"{name} was added.") def delete_contact(addressbook): - pass + pattern = input("Enter a part of their name: ").strip() + + for i, contact in enumerate(addressbook): + # check each contact if it contains the substring + if pattern in contact["name"]: + deleted_name = contact["name"] + addressbook.pop(i) + print(f"{deleted_name} was deleted") + + return + # if we reach the end of the iteration, we have not found a match + print("Contact not found!") diff --git a/python/src/main.py b/python/src/main.py index 7bd07c8..32f09dd 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -1,11 +1,39 @@ import contacts -addressbook = [] +addressbook = [ + { "name": 'Bruce Wayne', "phone": '555-123-4567', "email": 'bruce@wayne.com' }, + { "name": 'Clark Kent', "phone": '555-222-3333', "email": 'clark@dailyplanet.com' }, + { "name": 'Diana Prince', "phone": '555-444-5555', "email": 'diana@amazon.com' } +] def menu(): - pass + print('[1] Display all contacts') + print('[2] Add a new contact') + print('[3] Delete a contact') + print('[4] Exit') def main(): - pass + run = True + while run: + try: + menu() + selection = int(input("Enter a selection: ")) + + match selection: + case 1: + contacts.show_contacts(addressbook) + case 2: + contacts.add_contact(addressbook) + case 3: + contacts.delete_contact(addressbook) + case 4: + run = False + case _: + print(f"Invalid selection: {selection}") + except ValueError: + print("in valid integer input") + + print("Goodbye!") + main()