From 43e8c69e82f9216df4358eeb74c290ec86a87e20 Mon Sep 17 00:00:00 2001 From: Abay Date: Fri, 8 Dec 2023 10:46:04 -0700 Subject: [PATCH 1/2] so far so good --- python/src/contacts.py | 23 ++++++++++++++++++++--- python/src/main.py | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/python/src/contacts.py b/python/src/contacts.py index 4dce478..d784431 100644 --- a/python/src/contacts.py +++ b/python/src/contacts.py @@ -1,8 +1,25 @@ def show_contacts(addressbook): - pass + print('Showing all contacts: ') + 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'\n{name} was added.\n') def delete_contact(addressbook): - pass + pattern = input('Enter a part of their name: ').strip() + idx = None + for address in range(len(addressbook)): + print('ay') \ No newline at end of file diff --git a/python/src/main.py b/python/src/main.py index 7bd07c8..d65aa1f 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(): + print('[1] Display all contacts'); + print('[2] Add a new contact'); + print('[3] Delete a contact'); + print('[4] Exit'); pass 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(f"Invalid integer input: {selection}") + continue + print('Goodbye.') main() From 9d7f410985c0a5c956ec0304545813ba5a28ad40 Mon Sep 17 00:00:00 2001 From: Abay Date: Fri, 8 Dec 2023 11:05:12 -0700 Subject: [PATCH 2/2] complete --- python/src/contacts.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/python/src/contacts.py b/python/src/contacts.py index d784431..210756e 100644 --- a/python/src/contacts.py +++ b/python/src/contacts.py @@ -20,6 +20,9 @@ def add_contact(addressbook): def delete_contact(addressbook): pattern = input('Enter a part of their name: ').strip() - idx = None - for address in range(len(addressbook)): - print('ay') \ No newline at end of file + for i, contact in enumerate(addressbook): + if pattern.lower() in contact["name"].lower(): + print(f'{contact["name"]} has been deleted.') + addressbook.pop(i) + return + print('Contact not found') \ No newline at end of file