Learning Objectives
In this challenge you will improve your string manipulation techniques as well as use Python to perform some basic mathematical calculations.
Did you know?
The molecular weight (mass) may be calculated from the molecular formula of the substance; it is the sum of the atomic weights of the atoms making up the molecule. The molecular mass is expressed in atomic mass unit (amu).
For example, water has the molecular formula H2O, indicating that there are two atoms of hydrogen and one atom of oxygen in a molecule of water.
Atom | Atomic Mass | Quantity | Total Mass |
H | 1.008 | 2 | 2.016 |
O | 15.999 | 1 | 15.999 |
H20 | 18.015 |
In the following script we use a Python dictionary called atomicMass to store the atomic mass of some of the main atoms (C, H, O…)
We then work out the molecular mass of water: H20.
Your Challenge
Adapt this script to get the program ask the end-user to enter a molecular formula of their choice.
Using string manipulation techniques your Python script will break down the formula entered by the end-user and calculate the total mass of the molecule.
Test your script with the following molecules:
Molecule | Formula |
Water | H2O |
Carbon Dioxide | CO2 |
Methane | CH4 |
Butane | C4H10 |
Carbonic Acid | H2CO3 |
Acetone | CH3COCH3 |
Lactose | C12H22O11 |
Challenge #2
Add more atoms to the atomMass dictionary as follows:
Atom | Atomic Mass |
Fe | 53.939 |
Ag | 106.905 |
Cu | 62.929 |
Na | 22.989 |
Cl | 34.968 |
Adapt your script so that it recognises when these atoms are used by the end-user when entering their molecular formula. This will be based on the use of uppercase & lowercase letters.
Test your script with the following molecules:
Molecule | Formula |
Sodium Chloride | NaCl |
Iron Oxide | Fe2O3 |
Copper Chloride Hydroxide | HOCuCl |
Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area