-
Notifications
You must be signed in to change notification settings - Fork 4
Fixes all tests #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,40 +5,53 @@ | |
|
|
||
|
|
||
| class Dessert(object): | ||
|
|
||
| def __init__(): | ||
| def __init__(self, price, calories=None): | ||
| # Edit me! | ||
| # You need to be able to initialize a Dessert object with arguments: | ||
| # price - required | ||
| # calories - optional | ||
|
|
||
| # This should set the object's price and calories, accessible by | ||
| # .price and .calories respectively. | ||
| """ For creating desserts """ | ||
| self.price = price | ||
| self.calories = calories | ||
| pass | ||
|
|
||
| # Add a calories_per_dollar method that returns the calories per dollar | ||
| # for the dessert. | ||
| def calories_per_dollar(self): | ||
| if self.calories != None: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A slightly more Pythony way would be |
||
| cal_per_dollar = self.calories / self.price | ||
| return cal_per_dollar | ||
| else: | ||
| return None | ||
|
|
||
| # Define a method is_a_cake on Dessert that returns False | ||
| def is_a_cake(self): | ||
| return False | ||
|
|
||
|
|
||
| class Cake(Dessert): | ||
|
|
||
| def __init__(): | ||
| price = 5 | ||
| calories = 200 | ||
| def __init__(self, kind): | ||
| # Edit me! | ||
| # Cakes all cost the same amount and have the same calories, so their | ||
| # price and calories can be set at the class-level, not during init. | ||
| # However, we need to be able to tell cakes apart. Accept argument: | ||
| # kind - required | ||
|
|
||
| self.kind = kind | ||
| pass | ||
|
|
||
| # Define a method is_a_cake on Cake that returns True | ||
| # (This will override the one on Dessert) | ||
| def is_a_cake(self): | ||
| if self.price == 5 and self.calories == 200: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A cake isn't defined by its price and calories, but at the object level, so if |
||
| return True | ||
|
|
||
|
|
||
| class Menu(object): | ||
|
|
||
| def __init__(self, items): | ||
| self.items = items | ||
|
|
||
|
|
@@ -49,3 +62,18 @@ def desserts(self): | |
| if isinstance(item, Dessert): | ||
| desserts.append(item) | ||
| return desserts | ||
|
|
||
| def cakes(self): | ||
| cakes = [] | ||
| for item in self.items: | ||
| if isinstance(item, Cake): | ||
| cakes.append(item) | ||
| return cakes | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,17 +79,19 @@ def test_object_relationships(): | |
| # NOTE: To test that it really works, you probably want to create a Menu | ||
| # with a list that includes things that *aren't* desserts, like integers. | ||
|
|
||
| assert False # Take this line out, it forces the test to fail | ||
| # assert False # Take this line out, it forces the test to fail | ||
|
|
||
| # Create a cakes() method that does the same thing. | ||
| # This code is the test for cakes(): | ||
|
|
||
| dessert1 = Dessert(price=10) | ||
| dessert2 = Dessert(price=12) | ||
| dessert3 = Dessert(price=13) | ||
| dessert4 = 5 | ||
| dessert5 = 'cat' | ||
| cake1 = Cake(kind='sponge') | ||
| cake2 = Cake(kind='birthday') | ||
| my_desserts = [dessert1, dessert2, dessert3, cake1, cake2] | ||
| my_desserts = [dessert1, dessert2, dessert3, dessert4, dessert5, cake1, cake2] | ||
| my_menu = Menu(my_desserts) | ||
| cakes = my_menu.cakes() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should also be testing the |
||
| # There should only be two items in cakes! | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
passis really just a placeholder when you want to have something inside an indent but haven't written what it is yet. once you have other code in there, you can delete thepass.