From 7e65f3956537090aaa52fbdcff3a271a61e9a48e Mon Sep 17 00:00:00 2001 From: carldamey Date: Sat, 2 Dec 2023 00:18:05 -0800 Subject: [PATCH 1/4] finishes main.py and the first 2 functions of contacts.py --- python/src/contacts.py | 19 +++++++++++++++++-- python/src/main.py | 29 +++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/python/src/contacts.py b/python/src/contacts.py index 4dce478..c1cc5ce 100644 --- a/python/src/contacts.py +++ b/python/src/contacts.py @@ -1,8 +1,23 @@ 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"\n{name} was added.\n") def delete_contact(addressbook): pass diff --git a/python/src/main.py b/python/src/main.py index 7bd07c8..b412f6a 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -1,11 +1,32 @@ 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 = 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("Goodbye!") + main() From aa94cf99e4d024c6f59991401764c99f0cf46d79 Mon Sep 17 00:00:00 2001 From: carldamey Date: Sat, 2 Dec 2023 00:24:03 -0800 Subject: [PATCH 2/4] creates delete_contact function --- python/src/contacts.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/python/src/contacts.py b/python/src/contacts.py index c1cc5ce..7b32a05 100644 --- a/python/src/contacts.py +++ b/python/src/contacts.py @@ -20,4 +20,18 @@ def add_contact(addressbook): print(f"\n{name} was added.\n") def delete_contact(addressbook): - pass + pattern = prompt("Enter a part of their name: ").strip() + idx = null + + for i in range(0, len(addressbook)): + if addressbook[i]['name'].index(pattern) >= 0: + idx = i + + if idx == null: + print("\nContact not found!\n") + return + + deleted_name = addressbook[idx]['name'] + addressbook.remove(idx) + + console.log(f"\n{deleted_name} was deleted.") \ No newline at end of file From b8f272daff67b051f1bf187ec12acaeddc2b6f58 Mon Sep 17 00:00:00 2001 From: carldamey Date: Sat, 2 Dec 2023 00:30:42 -0800 Subject: [PATCH 3/4] fixes various bugs in delete_contact function --- python/src/contacts.py | 12 ++++++------ python/src/main.py | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/python/src/contacts.py b/python/src/contacts.py index 7b32a05..52cae4c 100644 --- a/python/src/contacts.py +++ b/python/src/contacts.py @@ -20,18 +20,18 @@ def add_contact(addressbook): print(f"\n{name} was added.\n") def delete_contact(addressbook): - pattern = prompt("Enter a part of their name: ").strip() - idx = null + pattern = input("Enter a part of their name: ").strip() + idx = None for i in range(0, len(addressbook)): - if addressbook[i]['name'].index(pattern) >= 0: + if addressbook[i]['name'].find(pattern) >= 0: idx = i - if idx == null: + if idx == None: print("\nContact not found!\n") return deleted_name = addressbook[idx]['name'] - addressbook.remove(idx) + addressbook.remove(addressbook[idx]) - console.log(f"\n{deleted_name} was deleted.") \ No newline at end of file + print(f"\n{deleted_name} was deleted.") \ No newline at end of file diff --git a/python/src/main.py b/python/src/main.py index b412f6a..f0e8f3a 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -16,7 +16,7 @@ def main(): run = True while run: menu() - selection = input("Enter a selection: ") + selection = int(input("Enter a selection: ")) if selection == 1: contacts.show_contacts(addressbook) elif selection == 2: @@ -28,5 +28,5 @@ def main(): else: print("\nThat selection is not valid, please try again!\n") print("Goodbye!") - + main() From 0e44156a5d7585ff2e81b043f09bcf3d3a57dd12 Mon Sep 17 00:00:00 2001 From: carldamey Date: Sat, 2 Dec 2023 00:47:29 -0800 Subject: [PATCH 4/4] completes deliverable by fixing typos in main.py and contacts.py --- python/src/contacts.py | 6 +++--- python/src/main.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/python/src/contacts.py b/python/src/contacts.py index 52cae4c..1636ddc 100644 --- a/python/src/contacts.py +++ b/python/src/contacts.py @@ -1,7 +1,7 @@ def show_contacts(addressbook): print() for contact in addressbook: - print(f"{contact['name']} ({contact['email']}): ${contact['phone']}") + print(f"{contact['name']} ({contact['email']}): {contact['phone']}") print() def add_contact(addressbook): @@ -28,10 +28,10 @@ def delete_contact(addressbook): idx = i if idx == None: - print("\nContact not found!\n") + print("\nContact Not found!\n") return deleted_name = addressbook[idx]['name'] addressbook.remove(addressbook[idx]) - print(f"\n{deleted_name} was deleted.") \ No newline at end of file + print(f"\n{deleted_name} was deleted.\n") \ No newline at end of file diff --git a/python/src/main.py b/python/src/main.py index f0e8f3a..ca3a146 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -27,6 +27,6 @@ def main(): run = False else: print("\nThat selection is not valid, please try again!\n") - print("Goodbye!") + print("\nGoodbye!\n") main()