diff --git a/python/beginner/profile_lookup_vinimlo.py b/python/beginner/profile_lookup_vinimlo.py new file mode 100644 index 0000000..fce8f11 --- /dev/null +++ b/python/beginner/profile_lookup_vinimlo.py @@ -0,0 +1,67 @@ +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): + + boolField = False + fieldValue = None + + for title, value in item.items(): + if title == field: + boolField = True + fieldValue = value + + return boolField, fieldValue + +look_up_profile("Akira", "like")