Introduction
Scaling a recipe is a common need when cooking for a different number of people than the original recipe suggests.
Whether you’re cooking for a small group or a large gathering, it’s important to adjust the ingredient amounts accordingly.
This Python program helps you scale a recipe up or down based on the number of servings you need.
Objective
The goal of this program is to provide a way to adjust the quantities of ingredients in a recipe based on the desired
number of servings. You simply input the original number of servings and the desired number, and the program will
calculate the correct amounts for each ingredient.
Python Code
# Recipe Scaling Program in Python def scale_recipe(ingredients, original_servings, desired_servings): scaled_ingredients = {} scaling_factor = desired_servings / original_servings for ingredient, amount in ingredients.items(): scaled_ingredients[ingredient] = amount * scaling_factor return scaled_ingredients # Example usage if __name__ == "__main__": # Original recipe (amounts are for 4 servings) ingredients = { 'flour (cups)': 2, 'sugar (cups)': 1, 'butter (tablespoons)': 4, 'eggs': 2 } original_servings = 4 desired_servings = int(input("Enter the number of servings you want: ")) scaled_ingredients = scale_recipe(ingredients, original_servings, desired_servings) print("\nScaled Recipe Ingredients for", desired_servings, "servings:") for ingredient, scaled_amount in scaled_ingredients.items(): print(f"{ingredient}: {scaled_amount}")
Program Explanation
The program begins by defining a function called scale_recipe
that takes three parameters:
- ingredients: A dictionary containing the ingredients and their amounts for the original recipe.
- original_servings: The number of servings the original recipe makes.
- desired_servings: The number of servings you want to scale the recipe for.
The function calculates a scaling factor by dividing the desired servings by the original servings. It then iterates over the
ingredients
dictionary and multiplies each ingredient’s amount by the scaling factor to obtain the new quantity.
The program then prints the scaled ingredient amounts for the desired servings.
How to Run the Program
-
- Copy the code into a Python file (e.g.,
scale_recipe.py
). - Make sure you have Python installed on your computer.
- Open a terminal or command prompt, navigate to the folder containing the Python file, and run the script:
- Copy the code into a Python file (e.g.,
python scale_recipe.py
- Enter the desired number of servings when prompted.
- The program will display the scaled ingredient amounts for the new number of servings.