Question: Part B Here is solution code for the Recipe class from last time. To simplify the code, we have removed the input checking in the

Part B
Here is solution code for the Recipe class from last time. To simplify the code, we have removed the input checking in the _init _ method, as well
as the _ method.
In []: class Recipe:
def (self, title, ingredients, directions):
self.title = title
self.ingredients = ingredients
self.directions = directions
def_rmul_(self, multiplier):
multiplied_ingredients ={key : multiplier*val for key, val in self.ingredients.items()}
return Recipe(self.title, multiplied_ingredients, self.directions)
Now, implement subtraction in which the first argument is a Pantry and the second argument is a Recipe . The relevant magic method for this is
called _(), and should be implemented in the Pantry class. Here's how subtraction my_pantry - my_recipe should work:
You may assume that my_pantry and my_recipe are valid instances of their class. In particular, all quantities of ingredients are positive numbers
( int s or float s).
If all keys from my_recipe. ingredients are present in my_pantry, and if they all have values smaller than their values in my_pantry, then the
result of my_pantry - my_recipe is a new Pantry object in which the values corresponding to the keys have been reduced by the quantity in
my_recipe.
If a key is present in my_pantry but not in my_recipe, then it is treated as though it is present in my_pantry with value 0.
For now, you can assume that the conditions of clause 2. are met, and that subtraction should therefore "work." That is, you can assume that you have
enough of all ingredients in the pantry to make the recipe. For example, with my_pantry from Part A,
title = "cookies"
ingredients
"flour (grams)": 400,
"butter (grams)" : 200,
"salt (arams)" : 10.
 Part B Here is solution code for the Recipe class from

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!