Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Phosgene is COCl2, and cobalt chloride is CoCl2. Write what re.findall(r"([A-Z][a-z]?)(\d*)", f) gives for each. Then find the molar mass of phosgene. Use C , O and Cl . What happens if you ask for the molar mass of CoCl2 and cobalt is not in the dictionary?
Molar mass the sum of atomic mass times count for every element. In Python: sum(atomic[el] * n for el, n in counts.items()). A formula is read into symbols and counts with re.findall(r"([A-Z][a-z]?)(\d*)", formula), after brackets are opened. Mass percent .
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Step 1. Phosgene. [('C', ''), ('O', ''), ('Cl', '2')]: carbon, oxygen and two chlorine.
Step 2. Cobalt chloride. [('Co', ''), ('Cl', '2')]: the small o belongs to the symbol Co.
Step 3. Molar mass of phosgene. g/mol.
Step 4. Cobalt missing. atomic['Co'] stops the program with KeyError: 'Co'. Add cobalt to the dictionary first.
Answer: g/mol for phosgene; the case of the letters decides the elements.
Log in to join the discussion.
Loading…