Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 ยท Projects and Practice

Project: Build a Stoichiometry Calculator

Build a working stoichiometry tool in Python: parse chemical formulas, compute molar masses, balance equations automatically and solve limiting reagent problems.

In this project you will turn everything from the track into a small program: a calculator that reads a chemical formula, computes its molar mass, balances an equation, and finds the limiting reagent and the theoretical yield. Building the tool is a good way to check that you understand each step, because a program cannot guess what you mean.

The code runs in your browser. It is a learning tool built from standard atomic masses, not a replacement for a lab database, and it handles simple formulas only (elements, subscripts and parentheses; no hydrates written with a dot, and no charges).

Step 1: parse a formula and get its molar mass

A formula such as Ca(OH)2 is a list of element symbols with counts, where parentheses multiply what is inside. We read it with a small stack: when we see (, we start a new group; when we see ), we multiply the group by the number that follows and merge it into the group below.

Python Step 1: formula parser and molar mass

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.

Step 2: balance an equation automatically

Balancing means finding whole-number coefficients so that every element has the same atom count on both sides. This is a linear algebra problem: make a matrix with one row per element and one column per substance (reactants positive, products negative). Coefficients that balance the equation are in the null space of the matrix, meaning the vector c with A c = 0. We use the sympy library, which finds the null space exactly with fractions, and then scale to the smallest whole numbers.

Python Step 2: automatic equation balancer

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 last equation has several elements and is hard to balance by inspection, yet the null-space method handles it directly. The solution is unique only if the null space has dimension 1; some "equations" have several valid balances, and the program says so rather than guessing.

Step 3: solve a limiting-reagent problem

Now combine the pieces. Given a balanced reaction, masses of the reactants and an actual yield, the program should report the limiting reagent, the theoretical yield of a chosen product, the excess left over, and the percent yield.

flowchart TD
    A["Formulas and masses"] --> B["Molar masses"]
    B --> C["Moles of each reactant"]
    C --> D["Balance the equation"]
    D --> E["Divide by coefficients:<br/>find limiting reagent"]
    E --> F["Theoretical yield,<br/>excess, percent yield"]

The data flow of the finished calculator.

Python Step 3: complete stoichiometry solver

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 "22.0 g collected" is an invented value to show how percent yield is reported; it is not an experimental result.

Extension ideas

  1. Join Steps 2 and 3: call the balancer first, so the user only types formulas.
  2. Add more elements: extend the atomic-mass table; check values against a periodic table.
  3. Add solutions: accept molarity and volume instead of mass.
  4. Add gases: add the ideal gas law to report the volume of a gas product.
  5. Add error handling: unknown element symbols, unbalanced input and negative masses should give clear messages.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Why does the balancer use the null space of a matrix?

    Each row says 'atoms on the left minus atoms on the right is zero'; the solutions of A c = 0 are the balancing coefficients.
  2. 2. How does the formula parser handle the subscript after a closing parenthesis, as in Ca(OH)2?

    The subscript 2 applies to both O and H inside the parentheses, giving 2 O and 2 H.
  3. 3. Which value decides the limiting reagent in the solver?

    The reactant that supports the fewest recipes is limiting.

Summary

  • A formula parser with a stack handles parentheses and multiplies group counts.
  • Molar mass is the sum of atomic masses times atom counts.
  • Balancing is a null-space problem on the element-count matrix.
  • The solver follows the same method you use on paper: moles, divide by coefficients, limiting reagent, theoretical yield, percent yield.
  • A program is a good test of your understanding, because every assumption must be explicit.

Back to the track: Chemistry: Reactions and Stoichiometry