friend_fcuntion

I was stuck on def people_who_like_color(people, color) and def get_color_dict(people); I forgot to define the list or dict first;
Or sometimes I used the wrong label for them. I checked the output when I test it and also ask ChatGPT to identify what's wrong with my code.
This commit is contained in:
grace-xing6 2024-10-16 20:29:12 -04:00
parent b874f4df86
commit 51e76315fa
1 changed files with 35 additions and 6 deletions

View File

@ -17,7 +17,10 @@ def count_people(people):
>>> count_people(friends) >>> count_people(friends)
10 10
""" """
raise NotImplementedError() n = 0
for i in people:
n = n + 1
return n
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 +30,10 @@ def get_email(people, name):
>>> get_email(friends, "Tad Winters") >>> get_email(friends, "Tad Winters")
None None
""" """
raise NotImplementedError() for person in people:
if person["name"] == name:
return person["email"]
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.
@ -37,7 +43,10 @@ def count_favorite_colors(people, name):
>>> count_favorite_colors(family, "Raphael Chambers") >>> count_favorite_colors(family, "Raphael Chambers")
1 1
""" """
raise NotImplementedError() for person in people:
if person["name"] == name:
return len(person["favorite_colors"])
return None # If the person is not found
def people_who_like_color(people, color): def people_who_like_color(people, color):
"""Returns a list containing only those people who like the given color. """Returns a list containing only those people who like the given color.
@ -57,7 +66,11 @@ def people_who_like_color(people, color):
>>> people_who_like_color(family, "indigo") >>> people_who_like_color(family, "indigo")
[] []
""" """
raise NotImplementedError() matching_people = []
for person in people:
if color in person["favorite_colors"]:
matching_people.append(person)
return matching_people
def count_people_who_like_color(people, color): def count_people_who_like_color(people, color):
"""Returns the number of people who like a given color. """Returns the number of people who like a given color.
@ -67,7 +80,13 @@ def count_people_who_like_color(people, color):
>>> count_people_who_like_color(family, "orange") >>> count_people_who_like_color(family, "orange")
1 1
""" """
raise NotImplementedError() n = 0
for person in people:
for colors in person["favorite_colors"]:
if colors == color:
n = n + 1
return n
def get_color_dict(people): def get_color_dict(people):
"""Returns a dict showing how many people like each color. """Returns a dict showing how many people like each color.
@ -86,7 +105,17 @@ def get_color_dict(people):
"orange": 1, "orange": 1,
} }
""" """
raise NotImplementedError() color_dict = {}
for person in people:
for color in person["favorite_colors"]:
if color in color_dict:
color_dict[color] = color_dict[color] + 1
else:
color_dict[color] = 1
return color_dict