Solution 1

``````# Define TooSweetException here
class TooSweetException(Exception):
pass

# Define TooManyCalsException here
class TooManyCalsException(Exception):
pass

def recipe_calculator(ingredients):
"""Calculates sweetness and calories of a recipe"""
total_sweetness = 0
total_calories = 0
MAX_SWEETNESS = 8
MAX_CALORIES = 800

for ingredient in ingredients:
total_sweetness += ingredient['sweetness']
total_calories += ingredient['calories']
if total_sweetness > MAX_SWEETNESS:
raise TooSweetException('Too sweet!')
if total_calories > MAX_CALORIES:
raise TooManyCalsException('Too many cals!')

# Don't delete this!
UNIT_OF_CHOCOLATE = {
'name': 'Chocolate',
'calories': 300,
'sweetness': 2
}

UNIT_OF_SUGAR = {
'name': 'Sugar',
'calories': 200,
'sweetness': 4
}

UNIT_OF_BUTTER = {
'name': 'Butter',
'calories': 250,
'sweetness': 1
}

UNIT_OF_WATER = {
'name': 'Water',
'calories': 0,
'sweetness': 0
}

UNIT_OF_FLOUR = {
'name': 'Flour',
'calories': 100,
'sweetness': 1
}

UNIT_OF_EGG = {
'name': 'Egg',
'calories': 50,
'sweetness': 0
}

UNIT_OF_BACON = {
'name': 'Bacon',
'calories': 350,
'sweetness': 3
}``````

# Healthy Recipes Only

Complete the function `recipe_calculator` so that it receives a list of recipe `ingredients` that have calories and a sweetness rating associated with each of them in the dictionaries in your main.py file.

Add up the total sweetness of all the items in the recipe and if they exceed a total sweetness of 8, raise a custom exception TooSweetException.

Then add up the total calories of all the items in the recipe and if they exceed a calorie total of 800, raise a custom exception TooManyCalsException.

If the recipe does not raise an exception, return a tuple of (total_sweetness, total_calories).

``````ingredients = [
UNIT_OF_EGG,
UNIT_OF_FLOUR,
UNIT_OF_WATER,
UNIT_OF_EGG,
UNIT_OF_BUTTER
]

recipe_calculator(ingredients) # (2, 450)

ingredients = [
UNIT_OF_BACON,
UNIT_OF_BUTTER,
UNIT_OF_BUTTER,
UNIT_OF_EGG,
UNIT_OF_EGG,
UNIT_OF_EGG
]

recipe_calculator(ingredients) # Raises TooManyCalsException
``````

### Test Cases

test recipe good -

``````def test_recipe_good():
ingredients = [
UNIT_OF_EGG,
UNIT_OF_FLOUR,
UNIT_OF_WATER,
UNIT_OF_EGG,
UNIT_OF_BUTTER
]
assert recipe_calculator(ingredients) == (2, 450)``````

test recipe too sweet -

``````import pytest

def test_recipe_too_sweet():
ingredients = [
UNIT_OF_CHOCOLATE,
UNIT_OF_SUGAR,
UNIT_OF_SUGAR,
UNIT_OF_SUGAR,
UNIT_OF_BUTTER
]
with pytest.raises(TooSweetException):
recipe_calculator(ingredients)
``````

test recipe too many cals -

``````import pytest

def test_recipe_too_many_cals():
ingredients = [
UNIT_OF_BACON,
UNIT_OF_BUTTER,
UNIT_OF_BUTTER,
UNIT_OF_EGG,
UNIT_OF_EGG,
UNIT_OF_EGG
]
with pytest.raises(TooManyCalsException):
recipe_calculator(ingredients)``````
# Define TooSweetException here # Define TooManyCalsException here def recipe_calculator(ingredients): """Calculates sweetness and calories of a recipe""" pass # Don't delete this! UNIT_OF_CHOCOLATE = { 'name': 'Chocolate', 'calories': 300, 'sweetness': 2 } UNIT_OF_SUGAR = { 'name': 'Sugar', 'calories': 200, 'sweetness': 4 } UNIT_OF_BUTTER = { 'name': 'Butter', 'calories': 250, 'sweetness': 1 } UNIT_OF_WATER = { 'name': 'Water', 'calories': 0, 'sweetness': 0 } UNIT_OF_FLOUR = { 'name': 'Flour', 'calories': 100, 'sweetness': 1 } UNIT_OF_EGG = { 'name': 'Egg', 'calories': 50, 'sweetness': 0 } UNIT_OF_BACON = { 'name': 'Bacon', 'calories': 350, 'sweetness': 3 }