Python

 

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

    1. Copy the code into a Python file (e.g., scale_recipe.py).
    2. Make sure you have Python installed on your computer.
    3. Open a terminal or command prompt, navigate to the folder containing the Python file, and run the script:
python scale_recipe.py
  1. Enter the desired number of servings when prompted.
  2. The program will display the scaled ingredient amounts for the new number of servings.
© 2025 Learn Programming

 

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 :)