From 9402bae7dde5d33bdb7e0610561d4ad5205740f5 Mon Sep 17 00:00:00 2001 From: wnf Date: Mon, 4 Dec 2023 16:45:20 -0600 Subject: [PATCH 1/3] completes exercise with one mysterious test failure for show_contacts even when function seems to be working fine. --- package-lock.json | 6 ++++++ python/src/contacts.py | 35 ++++++++++++++++++++++++++++++++--- python/src/main.py | 32 +++++++++++++++++++++++++++++--- 3 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..79ae03a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "2023-12-04-M4L3-python-containers-wbp", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} diff --git a/python/src/contacts.py b/python/src/contacts.py index 4dce478..8a9c76f 100644 --- a/python/src/contacts.py +++ b/python/src/contacts.py @@ -1,8 +1,37 @@ def show_contacts(addressbook): - pass + for contact in addressbook: + print('') + print(f"{contact['name']} ({contact['email']}): {contact['phone']}") + print('') def add_contact(addressbook): - pass + name = str(input('Enter name: ')).strip() + phone = str(input('Enter phone: ')).strip() + email = str(input('Enter email: ')).strip() + + new_contact = { + 'name': name, + 'phone': phone, + 'email': email + } + addressbook.append(new_contact) + + print(f"\n{new_contact['name']} was added.\n") def delete_contact(addressbook): - pass + pattern = str(input('Enter a part of their name: ')).strip() + + idx = '' + for i in range(len(addressbook)): + if pattern in addressbook[i]['name']: + idx = int(i) + + if idx == '': + print("\nContact Not Found!\n") + return + + deleted_name = addressbook[idx]['name'] + del addressbook[idx] + + print(f"\n{deleted_name} was deleted from address book list position {idx}.\n") + diff --git a/python/src/main.py b/python/src/main.py index 7bd07c8..823aa78 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -1,11 +1,37 @@ 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: + 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 + break + case _: + print('\nThat selection is not valid, please try again!\n') + + print('\nGoodbye!\n') main() From 9e57fef5469444da39df950cedcca740bca93e3e Mon Sep 17 00:00:00 2001 From: wnf Date: Mon, 4 Dec 2023 17:03:07 -0600 Subject: [PATCH 2/3] completes exercise with one mysterious test failure for show_contacts even when function seems to be working fine. --- python/src/contacts.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/src/contacts.py b/python/src/contacts.py index 8a9c76f..e31c157 100644 --- a/python/src/contacts.py +++ b/python/src/contacts.py @@ -27,11 +27,11 @@ def delete_contact(addressbook): idx = int(i) if idx == '': - print("\nContact Not Found!\n") + print("\nContact Not found!\n") return deleted_name = addressbook[idx]['name'] del addressbook[idx] - print(f"\n{deleted_name} was deleted from address book list position {idx}.\n") + print(f"\n{deleted_name} was deleted.\n") From d4c5eb3ddd25f749a4a229af3d1c8eaf51d2741e Mon Sep 17 00:00:00 2001 From: wnf Date: Fri, 8 Dec 2023 11:09:48 -0600 Subject: [PATCH 3/3] cmakes minor edits to clean up code --- python/src/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/src/main.py b/python/src/main.py index 823aa78..8c1df7d 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -21,7 +21,6 @@ def main(): match selection: case 1: contacts.show_contacts(addressbook) - case 2: contacts.add_contact(addressbook) case 3: