Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 39 ยท Projects and Practice

Project: A Recipe Scaler

Build a recipe scaler that uses ratio, proportion, fractions, unit conversion, rounding and unit prices to resize recipes and cost them, step by step in Python.

In this project you will build a small program that scales a recipe up or down and prints a tidy shopping list with a cost. It uses almost everything from this course: ratio and proportion (scale factors), fractions (cooking measures like 1/3 cup), unit conversion (grams to kilograms, milliliters to liters), rounding (sensible amounts) and unit prices (cost).

Cooks do this all the time: a recipe for 4 people is needed for 10. The idea is plain direct proportion: multiply every amount by the same factor. Let's build it in stages.

Stage 1: the scale factor

If a recipe serves 4 and you need 10 servings, the scale factor is 10 รท 4 = 2.5. Every ingredient is multiplied by 2.5. We use Fraction so that no accuracy is lost.

Python Stage 1: scale a recipe

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that 2 eggs ร— 2.5 = 5 is neat, but if you scale to 3 servings you would need 1.5 eggs. A real cook would round up or whisk an egg and use part of it. We will add that rule in Stage 3.

Stage 2: friendlier units

Large amounts read better in bigger units: 1,500 g is "1.5 kg", and 750 ml could be "0.75 L". The rule from Units and Conversions: divide by 1,000 to go up to the larger unit.

Python Stage 2: choose a readable unit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The function only changes the unit when the number reaches 1,000.

Stage 3: sensible rounding

Recipes are not precise to a hundredth of a gram. Decide a rounding rule for each unit:

  • Grams and milliliters: round to the nearest 5.
  • Items (eggs, apples): round up, since you cannot buy part of an egg.
  • Teaspoons: round to the nearest quarter.
Python Stage 3: round the amounts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python's round() rounds halves to the nearest even number (as explained in Rounding and Significant Figures); for cooking that is fine.

Stage 4: cost

Each ingredient has a unit price. Cost = amount ร— price per unit. Prices here are made up for the exercise.

Python Stage 4: cost the shopping list

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Using Decimal and rounding only at the end, we avoid the accumulating errors discussed in earlier lessons.

Challenges

  1. Ratio view. Flour to sugar is 300 : 100 = 3 : 1. Add a line that prints the flour:sugar ratio in simplest form after scaling; it should stay 3 : 1.
  2. Limiting ingredient. You only have 500 g of flour. What is the largest whole number of servings you can make? (Answer: divide 500 by 75 g per serving: 6 servings.)
  3. Convert the milk into US cups (1 US cup is about 236.6 ml).
  4. Best buy. Add a second flour pack (2 kg for 2.10) and decide which one is cheaper per gram.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. A recipe for 6 people uses 450 g of rice. How much rice for 10 people?

    The scale factor is 10 รท 6, so 450 ร— 10 / 6 = 750 g.
  2. 2. Why does the scaler round eggs up?

    Rounding up gives an overestimate, which avoids running short.
  3. 3. Which type of proportion is a recipe scale factor?

    Doubling the servings doubles every amount, so each ingredient is directly proportional to servings.

Summary

  • A recipe scale factor is servings wanted รท servings in the recipe, applied to every ingredient (direct proportion).
  • Fraction and Decimal keep arithmetic exact; round once at the end.
  • Pick readable units: grams to kilograms and milliliters to liters when numbers reach 1,000.
  • Choose a rounding rule that suits each item (up for whole items).
  • Cost is amount times unit price.

Next: Project: A Prime Explorer