friend_functions.py

This commit is contained in:
njmason2
2025-10-18 05:36:40 -04:00
parent 7f9e9bfb2a
commit a5e214b29f
2 changed files with 35 additions and 29 deletions

View File

@@ -20,22 +20,23 @@
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.
>>> get_email(family, "Tad Winters") # >>> get_email(family, "Tad Winters")
"ligula.aenean@hotmail.edu" # "ligula.aenean@hotmail.edu"
>>> get_email(friends, "Tad Winters") # >>> get_email(friends, "Tad Winters")
None # None
""" # """
for person in people: # for person in people:
if (person["name"]==name): # if (person["name"]==name): # on the left is the syntax
return person["email"] # # for a dict within a list lookup
# return person["email"] # dict within a list lookup
return None # return None # reached the end of the list without a match
#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.
>>> count_favorite_colors(family, "Tad Winters") >>> count_favorite_colors(family, "Tad Winters")
@@ -43,7 +44,12 @@ def get_email(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): # 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
#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.

View File

@@ -10,8 +10,8 @@ from unittest import TestCase, main
from people import friends, family from people import friends, family
from friend_functions import ( from friend_functions import (
# count_people, # count_people,
get_email, # get_email,
# count_favorite_colors, count_favorite_colors,
# people_who_like_color, # people_who_like_color,
# count_people_who_like_color, # count_people_who_like_color,
# get_color_dict, # get_color_dict,
@@ -22,22 +22,22 @@ from friend_functions import (
# self.assertEqual(count_people(family), 5) # self.assertEqual(count_people(family), 5)
# self.assertEqual(count_people(friends), 10) # self.assertEqual(count_people(friends), 10)
class TestGetEmail(TestCase): # class TestGetEmail(TestCase):
def test_returns_email_when_person_found(self): # def test_returns_email_when_person_found(self):
self.assertEqual(get_email(family, "Perry Calderon"), "mus.donec@outlook.org") # self.assertEqual(get_email(family, "Perry Calderon"), "mus.donec@outlook.org")
self.assertEqual(get_email(friends, "Joy Tate"), "risus.a.ultricies@hotmail.org") # self.assertEqual(get_email(friends, "Joy Tate"), "risus.a.ultricies@hotmail.org")
def test_returns_none_when_person_missing(self):
self.assertEqual(get_email(family, "Ken Kesey"), None)
# class TestCountFavoriteColors(TestCase):
# def test_returns_len_favorite_colors_when_person_found(self):
# self.assertEqual(count_favorite_colors(family, "Tad Winters"), 2)
# self.assertEqual(count_favorite_colors(friends,"Connor Rodriguez"), 4)
# self.assertEqual(count_favorite_colors(friends, "Yuli Reynolds"), 0)
# def test_returns_none_when_person_missing(self): # def test_returns_none_when_person_missing(self):
# self.assertEqual(count_favorite_colors(family, "Ken Kesey"), None) # self.assertEqual(get_email(family, "Ken Kesey"), None)
class TestCountFavoriteColors(TestCase):
def test_returns_len_favorite_colors_when_person_found(self):
self.assertEqual(count_favorite_colors(family, "Tad Winters"), 2)
self.assertEqual(count_favorite_colors(friends,"Connor Rodriguez"), 4)
self.assertEqual(count_favorite_colors(friends, "Yuli Reynolds"), 0)
def test_returns_none_when_person_missing(self):
self.assertEqual(count_favorite_colors(family, "Ken Kesey"), None)
# class TestPeopleWhoLikeColor(TestCase): # class TestPeopleWhoLikeColor(TestCase):
# def test_returns_correct_length(self): # def test_returns_correct_length(self):