From 57f82dca0dfa8c8add7d746d569fff4cb7a6a46f Mon Sep 17 00:00:00 2001 From: LeepDev Date: Fri, 4 Aug 2023 20:45:48 -0500 Subject: [PATCH 1/2] Finished --- python/src/contacts.py | 35 ++++++++++++++++++++++++++++++++--- python/src/main.py | 28 +++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/python/src/contacts.py b/python/src/contacts.py index 4dce478..abef7b4 100644 --- a/python/src/contacts.py +++ b/python/src/contacts.py @@ -1,8 +1,37 @@ 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() + + newContact = { + 'name': name, + 'phone': phone, + 'email': email + } + + addressbook.append(newContact) + + print(f'\n{name} was added.\n') def delete_contact(addressbook): - pass + pattern = input('Enter a part of their name: ').strip() + + idx = None + for i in range(len(addressbook)): + if pattern in addressbook[i]['name'] and addressbook[i]['name'].index(pattern) >= 0: + idx = i + + if idx == None: + print('\nContact Not found!\n'); + return + + deletedName = addressbook[idx]['name'] + addressbook.pop(idx) + + print(f'\n{deletedName} was deleted.\n') diff --git a/python/src/main.py b/python/src/main.py index 7bd07c8..0412c71 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -1,11 +1,33 @@ 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: ')) + + if selection == 1: + contacts.show_contacts(addressbook) + elif selection == 2: + contacts.add_contact(addressbook) + elif selection == 3: + contacts.delete_contact(addressbook) + elif selection == 4: + run = False + else: + print('\nThat selection is not valid, please try again!\n') + print('\nGoodbye!\n') main() From 283f5527d1d98c43b1e6a7a3a88ecbf823cca61e Mon Sep 17 00:00:00 2001 From: LeepDev Date: Mon, 7 Aug 2023 12:24:41 -0500 Subject: [PATCH 2/2] added lambda to main.py --- python/src/contacts.py | 3 ++- python/src/main.py | 34 ++++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/python/src/contacts.py b/python/src/contacts.py index abef7b4..caa06d6 100644 --- a/python/src/contacts.py +++ b/python/src/contacts.py @@ -24,8 +24,9 @@ def delete_contact(addressbook): idx = None for i in range(len(addressbook)): - if pattern in addressbook[i]['name'] and addressbook[i]['name'].index(pattern) >= 0: + if pattern in addressbook[i]['name']: idx = i + break if idx == None: print('\nContact Not found!\n'); diff --git a/python/src/main.py b/python/src/main.py index 0412c71..3ececed 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -12,22 +12,40 @@ def menu(): print('[3] Delete a contact') print('[4] Exit') + def main(): + def quit(addressbook): + nonlocal run + run = False run = True while run: menu() selection = int(input('Enter a selection: ')) - if selection == 1: - contacts.show_contacts(addressbook) - elif selection == 2: - contacts.add_contact(addressbook) - elif selection == 3: - contacts.delete_contact(addressbook) - elif selection == 4: - run = False + # if selection == 1: + # contacts.show_contacts(addressbook) + # elif selection == 2: + # contacts.add_contact(addressbook) + # elif selection == 3: + # contacts.delete_contact(addressbook) + # elif selection == 4: + # run = False + # else: + # print('\nThat selection is not valid, please try again!\n') + + switch = { + 1: lambda addressbook: contacts.show_contacts(addressbook), + 2: lambda addressbook: contacts.add_contact(addressbook), + 3: lambda addressbook: contacts.delete_contact(addressbook), + 4: quit + } + + if selection in switch: + switch[selection](addressbook) else: print('\nThat selection is not valid, please try again!\n') + print('\nGoodbye!\n') + main()