Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass is 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 the 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:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A slightly more Pythony way would be is not None rather than != None, though both work. :)

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:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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 is_a_cake() is called on a Cake then this bit of code gets run -- so we just needed this to return True instead of checking anything more.

return True


class Menu(object):

def __init__(self, items):
self.items = items

Expand All @@ -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








Binary file added objects.pyc
Binary file not shown.
6 changes: 4 additions & 2 deletions test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also be testing the desserts() method you added in here :)

# There should only be two items in cakes!
Expand Down