From add362152967b0d2b727366ef76fb21a8586dc17 Mon Sep 17 00:00:00 2001 From: asp Date: Mon, 7 Aug 2023 10:15:39 -0700 Subject: [PATCH 1/2] updates contacts --- python/src/contacts.py | 38 +++++++++++++++++++++++++++++++++++--- python/src/main.py | 34 +++++++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/python/src/contacts.py b/python/src/contacts.py index 4dce478..d15152e 100644 --- a/python/src/contacts.py +++ b/python/src/contacts.py @@ -1,8 +1,40 @@ 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: ") + phone = input("enter phone: ") + email = input("enter email: ") + + 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 i, contact in enumerate(addressbook): + # if pattern in contact["name"]: + # idx = i + + 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 + + deleted_name = addressbook.pop(idx) + + print(f"\n{deleted_name['name']} was deleted.\n") \ No newline at end of file diff --git a/python/src/main.py b/python/src/main.py index 7bd07c8..483e47b 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -1,11 +1,35 @@ 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 + def quit(addressbook): + nonlocal run + run = False + + while run: + selection = input("Enter a selection: ").strip() + + switch = { + "1": lambda addressbook : contacts.show_contacts(addressbook), + "2": lambda addressbook : contacts.add_contacts(addressbook), + "3": lambda addressbook : contacts.delete_contacts(addressbook), + "4": quit + } + + if selection in switch: + switch[selection](addressbook) + else: + print("\nThat selection is not valid, please try again!\n") main() + From 4c0640558792462fdc34ddd5924e2cdeeb4cfafb Mon Sep 17 00:00:00 2001 From: asp Date: Mon, 7 Aug 2023 10:17:57 -0700 Subject: [PATCH 2/2] updates main --- python/src/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/src/main.py b/python/src/main.py index 483e47b..70483b9 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -10,6 +10,7 @@ def menu(): print('[2] Add a new contact') print('[3] Delete a contact') print('[4] Exit') + def main(): run = True def quit(addressbook):