created the first two functions of friend_functions

I didn't properly read the instructions and I kept getting confused by what the data looked like so I created a trials
folder and I used it to dissect the data. Once it was clear that I was looking at a list of dictionaries, it became
very easy to create my first two functions. It's necessary to know what the dataset looks like structurally.
This commit is contained in:
ilmabura
2025-10-20 16:01:43 -04:00
parent b8fc500299
commit 2899a3250a
2 changed files with 105 additions and 5 deletions

View File

@@ -8,7 +8,7 @@
# Your job is to complete these functions. Remove the NotImplementedError from # Your job is to complete these functions. Remove the NotImplementedError from
# each and instead write code which returns the expected values. # each and instead write code which returns the expected values.
from people import *
def count_people(people): def count_people(people):
"""Counts the number of people. """Counts the number of people.
@@ -17,7 +17,9 @@ def count_people(people):
>>> count_people(friends) >>> count_people(friends)
10 10
""" """
raise NotImplementedError() count=len(people)
return count
def get_email(people, name): def get_email(people, name):
"""Returns the named person's email address. If there is no such person, returns None. """Returns the named person's email address. If there is no such person, returns None.
@@ -27,7 +29,11 @@ def get_email(people, name):
>>> get_email(friends, "Tad Winters") >>> get_email(friends, "Tad Winters")
None None
""" """
raise NotImplementedError() for n in people:
if n['name']== name:
return n['email']
else:
return None
def count_favorite_colors(people, name): def count_favorite_colors(people, name):
"""Returns the number of colors liked by the named person. If there is no such person, returns None. """Returns the number of colors liked by the named person. If there is no such person, returns None.
@@ -90,5 +96,4 @@ def get_color_dict(people):
print(get_email(friends, "Tad Winters"))

95
trials.py Normal file
View File

@@ -0,0 +1,95 @@
family = [
{
"name": "Tad Winters",
"email": "ligula.aenean@hotmail.edu",
"favorite_colors": ["aqua", "red"],
},
{
"name": "Raphael Chambers",
"email": "ac.eleifend.vitae@protonmail.com",
"favorite_colors": ["blue"],
},
{
"name": "Perry Calderon",
"email": "mus.donec@outlook.org",
"favorite_colors": ["black", "white", "grey"],
},
{
"name": "Walker Hurley",
"email": "auctor.odio@icloud.ca",
"favorite_colors": ["red", "yellow", "blue", "orange"],
},
{
"name": "Clementine Joseph",
"email": "hendrerit@aol.co.uk",
"favorite_colors": ["yellow", "aqua", "black"],
}
]
friends = [
{
"name": "Connor Rodriguez",
"email": "maecenas@yahoo.edu",
"favorite_colors": ["aqua", "teal", "sea foam", "turquoise"],
},
{
"name": "Rosalyn Hubbard",
"email": "elit.pharetra@google.edu",
"favorite_colors": ["red", "yellow", "black"],
},
{
"name": "Thomas Puckett",
"email": "sit.amet@aol.net",
"favorite_colors": ["blue"],
},
{
"name": "Yuli Reynolds",
"email": "augue.malesuada@google.edu",
"favorite_colors": [],
},
{
"name": "Joy Tate",
"email": "risus.a.ultricies@hotmail.org",
"favorite_colors": ["white", "grey", "sand"],
},
{
"name": "Prescott Price",
"email": "cursus.nunc@yahoo.edu",
"favorite_colors": ["red", "blue", "green"],
},
{
"name": "Josephine Keller",
"email": "nulla@protonmail.edu",
"favorite_colors": ["black"],
},
{
"name": "Isadora Carney",
"email": "sagittis.lobortis@protonmail.co.uk",
"favorite_colors": ["aqua", "lime"],
},
{
"name": "Jelani West",
"email": "vehicula.pellentesque.tincidunt@yahoo.org",
"favorite_colors": ["grey", "beige", "tan"],
},
{
"name": "Gay Pittman",
"email": "etiam.bibendum@yahoo.org",
"favorite_colors": ["yellow"],
}
]
def get_email(people, name):
"""Returns the named person's email address. If there is no such person, returns None.
>>> get_email(family, "Tad Winters")
"ligula.aenean@hotmail.edu"
>>> get_email(friends, "Tad Winters")
None
"""
for n in friends:
if n['name']=="Tad Winters":
print(n['email'])
else:
print(None)