From c06a1f28076a3ba5677b47ecc6c7edf278775373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Melo?= Date: Tue, 22 Oct 2019 07:35:57 -0300 Subject: [PATCH 1/2] Implements my solution --- python/beginner/profile_lookup_vinimlo.py | 64 +++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 python/beginner/profile_lookup_vinimlo.py diff --git a/python/beginner/profile_lookup_vinimlo.py b/python/beginner/profile_lookup_vinimlo.py new file mode 100644 index 0000000..3c773c2 --- /dev/null +++ b/python/beginner/profile_lookup_vinimlo.py @@ -0,0 +1,64 @@ +import json + +contacts = [ + { + "firstName": "Akira", + "lastName": "Laine", + "number": "0543236543", + "likes": ["Pizza", "Coding", "Brownie Points"] + }, + { + "firstName": "Harry", + "lastName": "Potter", + "number": "0994372684", + "likes": ["Hogwarts", "Magic", "Hagrid"] + }, + { + "firstName": "Sherlock", + "lastName": "Holmes", + "number": "0487345643", + "likes": ["Intriguing Cases", "Violin"] + }, + { + "firstName": "Kristian", + "lastName": "Vos", + "number": "unknown", + "likes": ["JavaScript", "Gaming", "Foxes"] + } +]; + +data = json.loads(json.dumps(contacts)) + +def look_up_profile(name, field): + + boolName = False + boolField = False + + for item in data: + for title, value in item.items(): + if title == "firstName": + firstName = value + if name == firstName: + boolName = True + boolField, fieldValue = getsField(item, field) + + if boolName: + if boolField: + return print(fieldValue) + else: + return print("No such property") + else: + return print("No such contact") + + pass + +def getsField(item, field): + + for title, value in item.items(): + if title == field: + boolField = True + fieldValue = value + + return boolField, fieldValue + +look_up_profile("Akira", "likes") From 040ebe5f8f679782beaf4ab1ae1fd6f466da33b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Melo?= Date: Tue, 22 Oct 2019 07:43:10 -0300 Subject: [PATCH 2/2] Fix --- python/beginner/profile_lookup_vinimlo.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/python/beginner/profile_lookup_vinimlo.py b/python/beginner/profile_lookup_vinimlo.py index 3c773c2..fce8f11 100644 --- a/python/beginner/profile_lookup_vinimlo.py +++ b/python/beginner/profile_lookup_vinimlo.py @@ -54,6 +54,9 @@ def look_up_profile(name, field): def getsField(item, field): + boolField = False + fieldValue = None + for title, value in item.items(): if title == field: boolField = True @@ -61,4 +64,4 @@ def getsField(item, field): return boolField, fieldValue -look_up_profile("Akira", "likes") +look_up_profile("Akira", "like")