-
-
Save jennielees/0751d30897fc83037583 to your computer and use it in GitHub Desktop.
Solution to objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Objects models file. | |
Edit the class definitions to make the tests pass. | |
""" | |
class Dessert(object): | |
def __init__(self, price, calories=None): | |
self.price = price | |
self.calories = calories | |
def calories_per_dollar(self): | |
if self.calories: | |
return self.calories / self.price | |
def is_a_cake(self): | |
return False | |
class Cake(Dessert): | |
price = 5 | |
calories = 200 | |
def __init__(self, kind): | |
self.kind = kind | |
def is_a_cake(self): | |
return True | |
class Menu(object): | |
def __init__(self, items): | |
self.items = items | |
def desserts(self): | |
# Return only the items in self.items which are desserts | |
desserts = [] | |
for item in self.items: | |
if isinstance(item, Dessert): | |
desserts.append(item) | |
return desserts | |
def cakes(self): | |
# Return only the items in self.items which are desserts | |
cakes = [] | |
for item in self.items: | |
if item.is_a_cake(): | |
cakes.append(item) | |
return cakes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment