Python

 

Introduction: Scaling a recipe is a common task in cooking. Whether you are cooking for a larger or smaller group, adjusting the ingredient quantities is crucial for achieving the same great taste. In this article, we will create a Python program to scale a recipe up or down based on the desired number of servings. You will learn how to write a simple yet effective program that can handle this task automatically.

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:

  1. 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.
  2. 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.
  3. 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

  1. Copy the code provided into a Python (.py) file, for example scale_recipe.py.
  2. Open your terminal or command prompt and navigate to the directory where the file is located.
  3. Run the program by typing python scale_recipe.py and hitting Enter.
  4. When prompted, enter the number of servings you want, and the program will display the adjusted ingredient quantities.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)