Course 4.1Lesson 2 of 3
A balanced equation fits naturally into two dictionaries, one for the reactants and one for the products. A function changes grams to moles, finds the limiting reagent as the smallest number of rounds of the equation, and returns the products and the excess. Then it is tested against a case worked by hand.
Try the code and the simulations beside the lesson.
Natural gas is mostly methane. It burns like this:
In Python, the coefficients fit into two dictionaries:
The keys are the formulas, and the values are the coefficients. The same code then works for any reaction: only the dictionaries change.
Think of the equation as a recipe. One round uses one and two . If you have of methane, it is enough for rounds. If you have of oxygen, it is enough for rounds.
The reactant with the fewest rounds runs out first: it is the limiting reagent. The number of rounds that really happen, the extent, is that smallest value. Then:
In Python, the limiting reagent is one line:
min with key=rounds.get compares the values but returns the key, here the formula CH4.
A chemist weighs grams, so the function changes grams to moles first and moles back to grams at the end. For gases at STP, one mole fills , so a volume is moles times .
The percent yield compares what you really collected with what the equation predicts:
A function that runs is not yet a function that is right. Test it on a case you can work by hand. For of methane in of oxygen, methane gives rounds and oxygen . Methane is limiting, so the program must say so:
assert stops the program with an AssertionError if the test fails, and does nothing if it passes.
The program burns of methane in of oxygen. It prints the limiting reagent, the products and what is left.
max instead of min. The reactant with the most rounds is in excess, not limiting.molar_mass["co2"] stops with KeyError: 'co2' when the key is "CO2".assert on a case you know.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
min({"A": 4, "B": 2}, key={"A": 4, "B": 2}.get) return?assert before you trust it?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.