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
20 changes: 15 additions & 5 deletions objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,48 @@
Edit the class definitions to make the tests pass.
"""


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

self.price = price
self.calories = calories
# This should set the object's price and calories, accessible by
# .price and .calories respectively.
pass

# Add a calories_per_dollar method that returns the calories per dollar
# for the dessert.
def calories_per_dollar(self):
calories_per_dollar = self.calories/self.price
print calories_per_dollar
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 also need to return something in here :)


# Define a method is_a_cake on Dessert that returns False

def is_a_cake(self):
return False


class Cake(Dessert):

def __init__():
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
super(Cake, self).__init__(price,calories)
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.

This probably causes an error because at this point, price and calories aren't defined. You also don't need to call the super method. Will walk through the solution in class!

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):
return True


class Menu(object):
Expand Down
Binary file added objects.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ 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():
Expand All @@ -91,7 +91,7 @@ def test_object_relationships():
cake2 = Cake(kind='birthday')
my_desserts = [dessert1, dessert2, dessert3, cake1, cake2]
my_menu = Menu(my_desserts)
cakes = my_menu.cakes()
cakes = my_menu.desserts()
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 can leave this in, still want to test this method!

# There should only be two items in cakes!
assert len(cakes) == 2
# The cakes should be sponge and birthday
Expand Down