From 10ba8e038b2f9fc72853bb2bef34f40666937964 Mon Sep 17 00:00:00 2001 From: drejmin Date: Mon, 7 Aug 2023 02:04:04 -0700 Subject: [PATCH 1/5] modified: python/src/main.py --- python/src/main.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/python/src/main.py b/python/src/main.py index 7bd07c8..8a6b8e6 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -1,11 +1,23 @@ +# //https://github.com/heapwolf/prompt-sync +promptSync = require('prompt-sync'); +prompt = promptSync({ sigint: true }); + 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 main() From 843391c120551e319ba2290571bdc47a2fecd534 Mon Sep 17 00:00:00 2001 From: drejmin Date: Mon, 7 Aug 2023 08:02:10 -0700 Subject: [PATCH 2/5] modified: python/src/contacts.py modified: python/src/main.py --- python/src/contacts.py | 34 +++++++++++++++++++++++++++++++--- python/src/main.py | 18 +++++++++++++----- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/python/src/contacts.py b/python/src/contacts.py index 4dce478..49f82e2 100644 --- a/python/src/contacts.py +++ b/python/src/contacts.py @@ -1,8 +1,36 @@ 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 = prompt('Enter name: ').strip(); + phone = prompt('Enter phone: ').strip(); + email = prompt('Enter email: ').strip(); + + newContact = { + name: name, + phone: phone, + email: email, + } + addressbook.push(newContact); + + print(f'\n{name} was added.\n') def delete_contact(addressbook): - pass + pattern = prompt('Enter a part of their name: ').strip(); + + idx = None; + for i in addressbook.len(): + if (addressbook[i]['name'].list.index(pattern)>=0): + idx =i; + + if (idx ==None): + print('\nContact Not Found\n') + return; + + deletedName = addressbook[idx]['name']; + addressbook.pop(idx,1); + + print(f'\n {deletedName} was deleted.\n') \ No newline at end of file diff --git a/python/src/main.py b/python/src/main.py index 8a6b8e6..46811a5 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -1,7 +1,3 @@ -# //https://github.com/heapwolf/prompt-sync -promptSync = require('prompt-sync'); -prompt = promptSync({ sigint: true }); - import contacts addressbook = [ @@ -17,7 +13,19 @@ def menu(): print('[4] Exit'); def main(): + run == True; + while run: + menu(); + selection = int(input('Enter a selection: ')); - pass + match selection: + case 1: contacts.showContacts(addressbook); break; + case 2: contacts.addContact(addressbook); break; + case 3: contacts.deleteContact(addressbook); break; + case 4: run == False; break; + case _: print('That selection is not valid, please try again!'); + + print('Goodbye') + main() From 48421a563b35bb4f7c615d4918adb352de4250ce Mon Sep 17 00:00:00 2001 From: drejmin Date: Mon, 7 Aug 2023 08:03:25 -0700 Subject: [PATCH 3/5] modified: python/src/contacts.py modified: python/src/main.py --- python/src/contacts.py | 8 ++++---- python/src/main.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/python/src/contacts.py b/python/src/contacts.py index 49f82e2..1cfb364 100644 --- a/python/src/contacts.py +++ b/python/src/contacts.py @@ -5,9 +5,9 @@ def show_contacts(addressbook): print(); def add_contact(addressbook): - name = prompt('Enter name: ').strip(); - phone = prompt('Enter phone: ').strip(); - email = prompt('Enter email: ').strip(); + name = input('Enter name: ').strip(); + phone = input('Enter phone: ').strip(); + email = input('Enter email: ').strip(); newContact = { name: name, @@ -19,7 +19,7 @@ def add_contact(addressbook): print(f'\n{name} was added.\n') def delete_contact(addressbook): - pattern = prompt('Enter a part of their name: ').strip(); + pattern = input('Enter a part of their name: ').strip(); idx = None; for i in addressbook.len(): diff --git a/python/src/main.py b/python/src/main.py index 46811a5..0a79aef 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -13,7 +13,7 @@ def menu(): print('[4] Exit'); def main(): - run == True; + run = True; while run: menu(); selection = int(input('Enter a selection: ')); @@ -22,7 +22,7 @@ def main(): case 1: contacts.showContacts(addressbook); break; case 2: contacts.addContact(addressbook); break; case 3: contacts.deleteContact(addressbook); break; - case 4: run == False; break; + case 4: run = False; break; case _: print('That selection is not valid, please try again!'); print('Goodbye') From b6b467a9ff15c3fa88797a24691fa1af89a0c1c9 Mon Sep 17 00:00:00 2001 From: drejmin Date: Mon, 7 Aug 2023 10:11:34 -0700 Subject: [PATCH 4/5] modified: python/src/main.py modified: python/src/contacts.py --- python/src/main.py | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/python/src/main.py b/python/src/main.py index 0a79aef..75115bc 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -1,29 +1,35 @@ import contacts 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' } + { '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(): - print('[1] Display all contacts'); - print('[2] Add a new contact'); - print('[3] Delete a contact'); - print('[4] Exit'); + print('[1] Display all contacts') + print('[2] Add a new contact') + print('[3] Delete a contact') + print('[4] Exit') def main(): - run = True; + run = True while run: - menu(); - selection = int(input('Enter a selection: ')); + menu() + selection = int(input('Enter a selection: ')) - match selection: - case 1: contacts.showContacts(addressbook); break; - case 2: contacts.addContact(addressbook); break; - case 3: contacts.deleteContact(addressbook); break; - case 4: run = False; break; - case _: print('That selection is not valid, please try again!'); + switch = { + 1: lambda addressbook: contacts.showContacts(addressbook); break, + 2: lambda addressbook: contacts.addContact(addressbook); break, + 3: lambda addressbook: contacts.deleteContact(addressbook); break, + 4: lambda addressbook: quit() + + + if selection in match: + match[selection]() + _: lambda addressbook: print('That selection is not valid, please try again!'); + + print('Goodbye') From 6c57ce464b0f5fa86dfb2154d06badac646f2281 Mon Sep 17 00:00:00 2001 From: drejmin Date: Mon, 7 Aug 2023 10:17:56 -0700 Subject: [PATCH 5/5] modified: python/src/contacts.py modified: python/src/main.py --- python/src/contacts.py | 37 ++++++++++++++++++------------------- python/src/main.py | 20 +++++++------------- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/python/src/contacts.py b/python/src/contacts.py index 1cfb364..51dc10a 100644 --- a/python/src/contacts.py +++ b/python/src/contacts.py @@ -1,36 +1,35 @@ def show_contacts(addressbook): print(); for contact in addressbook: - print(f'{contact.name} ({contact.email}): {contact.phone}'); - print(); + print(f'{contact.name} ({contact.email}): {contact.phone}') + print() def add_contact(addressbook): - name = input('Enter name: ').strip(); - phone = input('Enter phone: ').strip(); - email = input('Enter email: ').strip(); + name = input('Enter name: ').strip() + phone = input('Enter phone: ').strip() + email = input('Enter email: ').strip() - newContact = { - name: name, - phone: phone, - email: email, + new_contact = { + "name": name, + "phone": phone, + "email": email, } - addressbook.push(newContact); + addressbook.append(new_contact) print(f'\n{name} was added.\n') def delete_contact(addressbook): - pattern = input('Enter a part of their name: ').strip(); + pattern = input('Enter a part of their name: ').strip() - idx = None; - for i in addressbook.len(): - if (addressbook[i]['name'].list.index(pattern)>=0): - idx =i; + idx = None + for i in range(len(addressbook)): + if pattern in addressbook[i]['name']: + idx =i - if (idx ==None): - print('\nContact Not Found\n') + if idx == None: + print('\nContact Not found!\n') return; - deletedName = addressbook[idx]['name']; - addressbook.pop(idx,1); + deletedName = addressbook.pop(idx,1) print(f'\n {deletedName} was deleted.\n') \ No newline at end of file diff --git a/python/src/main.py b/python/src/main.py index 75115bc..bb3ba6a 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -18,19 +18,13 @@ def main(): menu() selection = int(input('Enter a selection: ')) - switch = { - 1: lambda addressbook: contacts.showContacts(addressbook); break, - 2: lambda addressbook: contacts.addContact(addressbook); break, - 3: lambda addressbook: contacts.deleteContact(addressbook); break, - 4: lambda addressbook: quit() - - - if selection in match: - match[selection]() - _: lambda addressbook: print('That selection is not valid, please try again!'); - - - + match selection: + case 1: contacts.show_contacts(addressbook) + case 2: contacts.add_contact(addressbook) + case 3: contacts.delete_contact(addressbook) + case 4: run = False + case _: print('\nThat selection is not valid, please try again!\n') + print('Goodbye')