From 149c144d7b3cd3b5b78ccffbd5c4e6b4b2544455 Mon Sep 17 00:00:00 2001 From: pkeen Date: Thu, 24 Aug 2023 19:38:47 +0100 Subject: [PATCH] WBP solved --- python/src/contacts.py | 39 ++++++++++++++++++++++++++++++++++++--- python/src/main.py | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/python/src/contacts.py b/python/src/contacts.py index 4dce478..41b1c71 100644 --- a/python/src/contacts.py +++ b/python/src/contacts.py @@ -1,8 +1,41 @@ 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)): + # indexOf() returns -1 if the substring is not found in the string + if pattern in addressbook[i]['name']: + idx = i + + if idx == None: + print('\nContact Not found!\n'); + return + + + deletedName = addressbook[idx]['name'] + del addressbook[idx] + + print(f"\n{deletedName} was deleted.\n") + + diff --git a/python/src/main.py b/python/src/main.py index 7bd07c8..6e530a5 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -1,11 +1,39 @@ -import contacts +from contacts import * -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: + show_contacts(addressbook) + break + case 2: + add_contact(addressbook) + break + case 3: + delete_contact(addressbook) + break + case 4: + run = False + break + case _: + print('\nThat selection is not valid, please try again!\n') + + print('\nGoodbye!\n') main()