Objective: The objective of this Python program is to enable users to input their desired number of servings and adjust the ingredient quantities of a recipe accordingly. This is a useful tool for both home cooks and professional chefs who need to adjust recipes for different group sizes.
Python Program to Scale a Recipe
# Recipe Scaling Program in Python
def scale_recipe(recipe, desired_servings):
# Calculate the scaling factor
scaling_factor = desired_servings / recipe['servings']
# Scale the ingredients
scaled_ingredients = {}
for ingredient, amount in recipe['ingredients'].items():
scaled_ingredients[ingredient] = round(amount * scaling_factor, 2)
# Output the scaled recipe
print(f"Scaled Recipe for {desired_servings} servings:")
for ingredient, scaled_amount in scaled_ingredients.items():
print(f"{ingredient}: {scaled_amount}")
print(f"Total Servings: {desired_servings}")
# Example recipe (ingredient amounts for 4 servings)
recipe = {
'servings': 4,
'ingredients': {
'flour (cups)': 2,
'sugar (cups)': 1,
'butter (tablespoons)': 4,
'eggs': 2
}
}
# Input the desired number of servings
desired_servings = int(input("Enter the desired number of servings: "))
# Scale the recipe based on the input
scale_recipe(recipe, desired_servings)
Explanation of the Program Structure
The Python program is divided into the following key parts:
- Function Definition: The
scale_recipe
function takes in the recipe and the desired number of servings. It calculates the scaling factor by dividing the desired servings by the original servings of the recipe. Then, it multiplies each ingredient amount by this factor to get the scaled amounts. - Input and Output: The program prompts the user to input the desired number of servings. It then calls the
scale_recipe
function and outputs the scaled ingredients and the number of servings to the console. - Recipe Data: The recipe is stored in a dictionary, where the number of servings and the ingredients (with their amounts) are specified. This data is used to calculate the scaled recipe.
How to Run the Program
- Copy the code provided into a Python (.py) file, for example
scale_recipe.py
. - Open your terminal or command prompt and navigate to the directory where the file is located.
- Run the program by typing
python scale_recipe.py
and hitting Enter. - When prompted, enter the number of servings you want, and the program will display the adjusted ingredient quantities.