friend_functions.py

This commit is contained in:
njmason2
2025-10-18 11:22:38 -04:00
parent a5e214b29f
commit 271b54e569
2 changed files with 39 additions and 30 deletions

View File

@@ -36,22 +36,22 @@
# return None # reached the end of the list without a match
def count_favorite_colors(people, name):
"""Returns the number of colors liked by the named person. If there is no such person, returns None.
# def count_favorite_colors(people, name):
# """Returns the number of colors liked by the named person. If there is no such person, returns None.
>>> count_favorite_colors(family, "Tad Winters")
2
>>> count_favorite_colors(family, "Raphael Chambers")
1
"""
for person in people:
if (person["name"]==name): # for a dict within a list lookup
return len(person["favorite_colors"]) # count of list elements in a dict within a list
# >>> count_favorite_colors(family, "Tad Winters")
# 2
# >>> count_favorite_colors(family, "Raphael Chambers")
# 1
# """
# for person in people:
# if (person["name"]==name): # for a dict within a list lookup
# return len(person["favorite_colors"]) # count of list elements in a dict within a list
return None # reached the end of the list without a match
# return None # reached the end of the list without a match
#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.
>>> people_who_like_color(family, "yellow")
[
@@ -69,7 +69,16 @@ def count_favorite_colors(people, name):
>>> people_who_like_color(family, "indigo")
[]
"""
# raise NotImplementedError()
fav_name=[] # initialize output list as None
for person in people:
if color in person["favorite_colors"]: # if color in the data file matches input color
fav_name.append(person) # append output list
# print(fav_name)
return fav_name # at the end of file, output list
#def count_people_who_like_color(people, color):
"""Returns the number of people who like a given color.