Session 12: Molecular Formula Calculator Project#

Introduction#

We are setting you a mid-semester project as a formative assessment - i.e., it doesn’t contribute to your module mark but provides an opportunity for you to practice for the real, end-of-semester coursework project and receive feedback from the teaching team.

Note

You don’t have to submit your project code, but unless you do (via the submission link on the module Moodle page, by the deadline given on that page) we won’t give you feedback.

We will also set aside a period at the end of the workshop for you to get together with a colleague and discuss your code. The purpose of this is for you to practice for the short interview you will have with one of the module team as part of the formal (summative) coursework assessment.

Project Overview#

Your brief is to develop a Python script that calculates the molecular weight of a molecule given its molecular formula as a string. This project integrates several programming concepts we’ve covered, including strings, loops, dictionaries, and control flow, and applies them to a practical chemistry problem.

Calculating molecular weight from a molecular formula is a fundamental task in chemistry. It involves parsing the formula to determine the number of each type of atom present and summing their atomic weights accordingly. This project will enhance your problem-solving skills and your ability to translate real-world scenarios into computational solutions.

An online search would quickly lead you to possible solutions, but for this exercise we want you to use a specific approach that only uses the elements of Python code that we have covered in the course up to now. It will therefore provide a pretty good test of how well you have assimilated the course material.

Objective: Write a Python script that:

  • Accepts a molecular formula as a string input (e.g., "H2O" for water).

  • Parses the string to identify each element and the corresponding number of atoms.

  • Uses a dictionary of atomic weights to look up the atomic weight of each element.

  • Calculates the total molecular weight by summing the products of atomic weights and their counts.

  • Handles both single-letter and two-letter element symbols (e.g., C for carbon, Cl for chlorine).

  • Is robust against various valid molecular formulas.

Planning Your Approach#

Before coding, it’s crucial to break down the problem into manageable steps. Here’s how you might approach it:

  1. Understand the Input Format:

    • Element Symbols: Elements are represented by one or two letters. The first letter is always uppercase, and the second letter (if present) is lowercase (e.g., H, He, Fe).

    • Atom Counts: An element symbol may be followed by a number indicating how many atoms are present. If no number is present, it implies one atom.

    Examples:

    • "H2O" represents 2 hydrogen atoms and 1 oxygen atom.

    • "C6H12O6" represents 6 carbon, 12 hydrogen, and 6 oxygen atoms.

    • "Fe2(SO4)3" represents a more complex formula with parentheses (advanced handling).

  2. Break Down the Problem:

    • Parsing the Molecular Formula:

      • Identify and extract element symbols and their counts from the string.

      • Handle both single-digit and multi-digit atom counts (e.g., H2, C12).

      • Account for one-letter and two-letter element symbols.

    • Storing Atomic Weights:

      • Create a dictionary mapping element symbols to their atomic weights.

      • Ensure your dictionary includes all elements that might appear in the formulas you test.

    • Calculating the Molecular Weight:

      • Multiply each element’s atomic weight by its atom count.

      • Sum these values to obtain the total molecular weight.

  3. Consider Edge Cases and Enhancements:

    • Implicit Atom Counts: If an element symbol is not followed by a number, assume the count is one.

    • Nested Groups (Optional): Handle formulas with parentheses for grouped atoms (e.g., "Fe2(SO4)3").

    • Invalid Input Handling: Decide how your script will respond to invalid formulas or unknown elements.

Building Up to the Full Solution#

To manage complexity, start by solving simpler versions of the problem and gradually build up to the full solution.

Step 1: Calculate Molecular Weight from a Dictionary#

Begin by writing a function that calculates the molecular weight when the molecule is provided as a dictionary of element symbols and their atom counts.

Step 2: Parse Formulas with Single-Letter Element Symbols#

Next, you might move to reading molecular formulas from strings, but only handle molecular formulas consisting only of elements with single-letter symbols (e.g., C, H, O, N).

Step 3: Extend Parsing to Two-Letter Element Symbols#

Enhance your script to handle elements with two-letter symbols.