Read sections 15.1, 15.2, 15.3, 15.4, 15.5, and 15.6 in the book and try out the code samples. You don't have to do any of the exercises.
Create a new class to model recipes:
class Recipe(object): """Models a recipe to make food."""
I can think of three attributes a recipe has: a name, a list of ingredients, and a list of instructions. (You can probably think of more.) Let's create a recipe and fill in these attributes.
>>> fake_chx_parm = Recipe() >>> fake_chx_parm.name = "Microwave Chicken Parm" >>> fake_chx_parm.ingredients = [] >>> fake_chx_parm.steps = [] >>> fake_chx_parm.ingredients.append("5 frozen chicken nuggets") >>> fake_chx_parm.ingredients.append("1/3 cup pasta sauce") >>> fake_chx_parm.ingredients.append("1 provolone slice") >>> fake_chx_parm.steps.append("Microwave the frozen chicken nuggets in a small bowl for 1 minute, 25 seconds.") >>> fake_chx_parm.steps.append("Pour the pasta sauce over one side of the heated nuggets.") >>> fake_chx_parm.steps.append("Place the provolone slice over the pasta sauce.") >>> fake_chx_parm.steps.append("Put everything back in the microwave for 25 more seconds, then enjoy!")
We could create another recipe:
>>> cup = Recipe() >>> cup.name = "PB Cup" >>> cup.ingredients = ["Peanut Butter Cup"] >>> cup.steps = ["Unwrap it and eat."]
It's important to be able to see what these look like. Let's create a function to print out our recipes:
def print_recipe(recipe): """Nicely prints a Recipe object.""" print("Recipe:", recipe.name) print() #Add a blank line print("Ingredients:") for ingredient in recipe.ingredients: print(" *", ingredient) print() #another blank line print("Steps:") for i in range(len(recipe.steps)): step = recipe.steps[i] print(" " + str(i) + ":", step)
Now try the code further above again to create recipes and call print_recipe on them! How does that look?