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
51 changes: 45 additions & 6 deletions objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,56 @@


class Dessert(object):

def __init__():
"""Makes a new dessert"""
def __init__(self, price, calories=None):
if calories is None:
self.calories = None
self.price = price
self.calories = calories
# 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.
pass
# pass

# Add a calories_per_dollar method that returns the calories per dollar
# for the dessert.

def calories_per_dollar(self):
if self.calories is None:
return None
return self.calories/self.price

# Define a method is_a_cake on Dessert that returns False

def is_a_cake(self):
return False

# new_dessert = Dessert(10)


class Cake(Dessert):

def __init__():
def __init__(self, kind):
self.kind = 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

pass

# pass
price = 5
calories = 200
# 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 All @@ -49,3 +68,23 @@ 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


# dessert1 = Dessert(price=10, calories=5)
# print dessert1.calories

# cake = Cake(kind="chocolate")
# print cake.kind

# my_desserts = [dessert1, cake]

# my_menu = Menu(my_desserts)

# cakes = my_menu.cakes()
2 changes: 1 addition & 1 deletion 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 Down