Implement a function ingredients in Python that takes a recipe and returns a dictionary of the ingredients needed. Details: accepts one multiline str argument containing a recipe each line of the recipe is either an instruction line or an ingredient line. The ingredient lines contain the character ':' , the instruction lines do not. instruction lines should be ignored ingredient lines will be structured like 'active dry yeast: 10g' . This breaks down as: an ingredient, e.g., 'active dry yeast' (can be multiple words) the character ':' followed by a space the quantity, e.g., '10g' , which will always be an integer (number of digits can vary) followed always by the letter 'g' ( for grams) note- that a particular ingredient may appear on more than one line, see sourdoughStarter below The function ingredients should output a dictionary: keys are ingredients each value is an int specifying the number grams of that ingredient used in the recipe if an ingredient appears on more than one line, the value should be the total amount of the ingredient required
Implement a function ingredients in Python that takes a recipe and returns a dictionary of the ingredients needed.
Details:
- accepts one multiline str argument containing a recipe
- each line of the recipe is either an instruction line or an ingredient line. The ingredient lines contain the character ':' , the instruction lines do not.
- instruction lines should be ignored
- ingredient lines will be structured like 'active dry yeast: 10g' . This breaks down as:
an ingredient, e.g., 'active dry yeast' (can be multiple words)
the character ':' followed by a space
the quantity, e.g., '10g' , which will always be an integer (number of digits can vary) followed always by the letter 'g' ( for grams)
- note- that a particular ingredient may appear on more than one line, see sourdoughStarter below
The function ingredients should output a dictionary:
- keys are ingredients
- each value is an int specifying the number grams of that ingredient used in the recipe
- if an ingredient appears on more than one line, the value should be the total amount of the ingredient required
Below is an example of a output from the code:
>>> ganache='semi-sweet chocolate: 224g\nheavy cream: 224g\nChop chocolate,
place in a bowl. Heat cream until it simmers.\nAdd cream to bowl, let stand for 3
minutes, then whisk until smooth.'
>>> print(ganache)
semi-sweet chocolate: 224g
heavy cream: 224g
Chop chocolate, place in a bowl. Heat cream until it simmers.
Add cream to bowl, let stand for 3 minutes, then whisk until smooth.
>>> ingredients(ganache)
{'semi-sweet chocolate': 224, 'heavy cream': 224}
>>> ingredients(ganache)['heavy cream']
224
>>> sourdoughStarter= 'Mix\nflour: 113g\nwater: 113g\nCover loosely and let sit
for 24 hours.\nDiscard 1/2 of the starter and add\nflour: 113g\nwater: 113g\nMix
and let sit for 24 hours. \nDiscard 1/2 of the starter and add\nflour:
113g\nwater: 113g\nMix and let sit for 24 hours. \nRepeat a bunch of times ...'
>>> ingredients(sourdoughStarter) # flour and water listed multiple times
{'flour': 339, 'water': 339}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images