08 Prove Assignment: Dictionaries
Purpose
Prove that you can write a Python program that uses a dictionary and lists.
Problem Statement
In chemistry, a mole is a huge quantity, specifically 602,214,076,000,000,000,000,000 (usually written as 6.02214076 × 1023). The molar mass of a substance is the mass in grams of one mole of the substance (grams / mole). A molar mass calculator is a program that computes the molar mass of a substance and the number of moles of a sample of that substance. To use a molar mass calculator, a chemist enters two inputs:
- The formula for a molecule, such as H2O (water) or C6H12O6 (glucose)
 - The mass in grams of a sample of the substance, such as 3.71
 
The calculator computes the molar mass of the molecule by doing the following for each element in the formula:
- Count the number of atoms of each element in the formula
 - Find the atomic mass of each element
 - Multiply the number of atoms by their atomic mass
 - Add the product to the total molar mass of the molecule
 
Then the calculator computes the number of moles in the sample with this formula:
Finally, the calculator prints two results for the chemist to see:
- the molar mass
 - the number of moles
 
Example
As an example, consider a sample of glucose (C6H12O6) with a mass of 12.37 grams. To use a molar mass calculator, a chemist enters
- C6H12O6
 - 12.37
 
The calculator computes the molar mass of glucose by doing the following:
- Sum the number of atoms of each element in the formula for glucose:
6 carbon atoms
12 hydrogen atoms
6 oxygen atoms - Find the atomic mass of each element:
Symbol Name Atomic Mass C Carbon 12.0107 H Hydrogen 1.00794 O Oxygen 15.9994  - Multiply the number of atoms by their atomic mass:
6 × 12.0107 = 72.0642 12 × 1.00794 = 12.09528 6 × 15.9994 = 95.9964  - Add the results of the multiplications to get the molar mass of glucose:72.0642 + 12.09528 + 95.9964 = 180.15588 grams/mole
 
Then the calculator divides the sample’s mass by the molar mass of glucose to calculate the number of moles in the sample:
The calculator prints two results for the chemist to see:
- the molar mass of glucose: 180.15588 grams/mole
 - the number of moles in the sample: 0.06866 moles
 
Assignment
During this assignment, you will write and test the remaining
    parts of the molar mass calculator that you started writing in the
    prove milestone from the previous lesson. When you are finished with
    this prove assignment, your chemistry.py program must
    contain at least three functions named as follows:
mainmake_periodic_tablecompute_molar_mass
Help
If you’re having trouble completing this assignment, reading related online documentation, using a generative AI as a tutor, or working with a human tutor will help you complete it.
Helpful Documentation
- The prove milestone of the previous lesson explains how a molar mass calculator should work.
 - The preparation content for this lesson explains how to create and use a dictionary in a Python program.
 - The preparation content for lesson 5 explains how to use
pytest,assert, andapproxto automatically verify that functions are correct. It also contains an example test function and links to additional documentation aboutpytest. 
Help from an AI Tutor
You can use a generative AI as a tutor to help you write and troubleshoot your program. Bro. Lee Barney created a custom Chat GPT named Pythonista that is designed to focus on Python functions, loops, if statements, and related programming concepts. If your program is generating an error, ask Pythonista a question like this:
I'm writing a Python program that computes the molecular mass and number of moles of a chemical sample. When I run my program, it causes the following error. Please suggest some mistakes that might cause this error.
(Copy and paste the error message here.)
You could also ask Pythonista a question about one of your functions, like this:
I wrote the following Python function that is supposed to (type a short description here). However, the function isn't (type what it's not doing correctly here). Please help me fix this function.
(Copy and paste your Python function here.)
Help from a Human Tutor
As a BYU-Idaho campus or online student you can get help from a tutor to complete your CSE 111 assignments. Each tutor is a current BYU-Idaho student employed by BYU-Idaho. Meeting with a tutor is free. It will not cost you any money to meet with a tutor. To get help from a tutor, you simply make an appointment and then meet with the tutor. Campus students meet with tutors in the tutoring center. Online students meet with tutors in Zoom. To make an appointment, follow the instructions in the course tutoring guide.
Steps
Do the following:
- Download the
formula.pyPython file and save it in the same folder where you saved yourchemistry.pyprogram from the previous prove milestone. Theformula.pyfile includes aFormulaErrorclass and a function namedparse_formula. Both of them are complete and work correctly, and you should not change them. - Open the
formula.pyfile in VS Code and read the triple quoted string at the top of theparse_formulafunction. As the triple quoted string states, theparse_formulafunction converts a chemical formula for a molecule, such as "C13H16N2O2" (melatonin), into a compound list, such as [["C", 13], ["H", 16], ["N", 2], ["O", 2]]. This compound list is known as a symbol_quantity_list because it contains the symbols of chemical elements and the quantity of atoms of each element that appear in a chemical formula. - Copy and paste the following import statement into your
chemistry.pyprogram at the top of your program. This statement will import theparse_formulafunction from theformula.pyfile into yourchemistry.pyprogram so that you can call theparse_formulafunction in your program.from formula import parse_formula
 - In your
chemistry.pyprogram, change the compound list that is in yourmake_periodic_tablefunction to a compound dictionary. Each item in the dictionary should use a chemical symbol as the key, and the value should be a list with the element’s name and atomic mass, like this:We strongly recommend that you use find and replace or multi-line editing in VS Code to quickly change the periodic table to a compound dictionary. If you don’t know how to use find and replace or multi-line editing, read this tutorial about multi-line editing or ask a fellow student, a tutor, a teaching assistant, or your teacher for help.periodic_table_dict = { # symbol: [name, atomic_mass] "Ac": ["Actinium", 227], "Ag": ["Silver", 107.8682], "Al": ["Aluminum", 26.9815386], ⋮ } - Copy and paste the following Python code into your
chemistry.pyprogram. Make sure you don’t paste the code inside an existing function.The code that you pasted includes the header and documentation string for a function named# Indexes for inner lists in the periodic table NAME_INDEX = 0 ATOMIC_MASS_INDEX = 1 # Indexes for inner lists in a symbol_quantity_list SYMBOL_INDEX = 0 QUANTITY_INDEX = 1 def compute_molar_mass(symbol_quantity_list, periodic_table_dict): """Compute and return the total molar mass of all the elements listed in symbol_quantity_list. Parameters symbol_quantity_list is a compound list returned from the parse_formula function. Each small list in symbol_quantity_list has this form: ["symbol", quantity]. periodic_table_dict is the compound dictionary returned from make_periodic_table. Return: the total molar mass of all the elements in symbol_quantity_list. For example, if symbol_quantity_list is [["H", 2], ["O", 1]], this function will calculate and return atomic_mass("H") * 2 + atomic_mass("O") * 1 1.00794 * 2 + 15.9994 * 1 18.01528 """ # Do the following for each inner list in the # compound symbol_quantity_list: # Separate the inner list into symbol and quantity. # Get the atomic mass for the symbol from the dictionary. # Multiply the atomic mass by the quantity. # Add the product into the total molar mass. # Return the total molar mass. returncompute_molar_mass. Read the docstring and comments in thecompute_molar_massfunction and write the code for that function. Note that you can complete thecompute_molar_massfunction by writing ten or fewer lines of code. - Modify the
mainfunction in yourchemistry.pyprogram so that it does the following:def main(): # Get a chemical formula for a molecule from the user. # Get the mass of a chemical sample in grams from the user. # Call the make_periodic_table function and # store the periodic table in a variable. # Call the parse_formula function to convert the # chemical formula given by the user to a compound # list that stores element symbols and the quantity # of atoms of each element in the molecule. # Call the compute_molar_mass function to compute the # molar mass of the molecule from the compound list. # Compute the number of moles in the sample. # Print the molar mass. # Print the number of moles. 
Call Graph
The following call graph shows the user-defined functions and
    function calls and returns as you should write them in your
    chemistry.py program. From this call graph we see the
    following function calls:
- The computer starts executing the
chemistry.pyprogram by calling themainfunction. - While executing the
mainfunction, the computer calls theinputandfloatfunctions. - While still executing the
mainfunction, the computer calls themake_periodic_table,parse_formula, andcompute_molar_massfunctions. - Finally, while still executing the
mainfunction, the computer calls theprintfunction. 
Testing Procedure
Verify that your program works correctly by following each step in this testing procedure:
- Download the
test_chemistry_2.pyPython file and save it in the same folder where you saved yourchemistry.pyprogram. Run thetest_chemistry_2.pyfile and ensure that all three of the test functions pass. If any of the test functions don’t pass, there is a mistake in yourchemistry.pyprogram. Read the output frompytest, fix the mistake, and run thetest_chemistry_2.pyfile again until all the test functions pass.> python test_chemistry_2.py =================== test session starts ==================== platform win32--Python 3.8.6, pytest-6.1.2, py-1.9.0, pluggy rootdir: C:\Users\cse111\lesson07 collected 3 items test_chemistry_2.py::test_make_periodic_table PASSED [ 33%] test_chemistry_2.py::test_parse_formula PASSED [ 66%] test_chemistry_2.py::test_compute_molar_mass PASSED [100%] ==================== 3 passed in 0.17s =====================
 - Run your finished
chemistry.pyprogram. Enter the input shown below and ensure that your program prints the output shown below.> python chemistry.py Enter the molecular formula of the sample: C6H6 Enter the mass in grams of the sample: 25.04 78.11184 grams/mole 0.32057 moles > python chemistry.py Enter the molecular formula of the sample: Al(OH)3 Enter the mass in grams of the sample: 17.03 78.00356 grams/mole 0.21832 moles > python chemistry.py Enter the molecular formula of the sample: Mg(C2H3O2)2 Enter the mass in grams of the sample: 21.94 142.39304 grams/mole 0.15408 moles
 
Exceeding the Requirements
If your program fulfills the requirements for this assignment as described in the previous prove milestone and the Assignment section above, your program will earn 93% of the possible points. In order to earn the remaining 7% of points, you will need to add one or more features to your program so that it exceeds the requirements. Here are a few suggestions for additional features that you could add to your program if you wish.
- Add a dictionary that contains known chemical formulas and their names. For example:Then write a function named
known_molecules_dict = { "Al(OH)3": "aluminum hydroxide", "Al2O3": "aluminum oxide", "CH3OH": "methanol", "C2H6O": "ethanol", "C2H5OH": "ethanol", "C3H8O": "isopropyl alcohol", "C3H8": "propane", "C4H10": "butane", "C6H6": "benzene", "C6H14": "hexane", "C8H18": "octane", "CH3(CH2)6CH3": "octane", "C13H18O2": "ibuprofen", "C13H16N2O2": "melatonin", "Fe2O3": "iron oxide", "FeS2": "iron pyrite", "H2O": "water" }get_formula_namewith the following header and documentation string.Call thedef get_formula_name(formula, known_molecules_dict): """Try to find formula in the known_molecules_dict. If formula is in the known_molecules_dict, return the name of the chemical formula; otherwise return "unknown compound". Parameters formula is a string that contains a chemical formula known_molecules_dict is a dictionary that contains known chemical formulas and their names Return: the name of a chemical formula """get_formula_namefunction from yourmainfunction and print the compound name for the user to see with the other output. - Add the atomic number for each element to the compound dictionary of elements. The atomic number of an element is the number of protons in the nucleus of that element. Write a function named
sum_protonswith the following header and documentation string.Call thedef sum_protons(symbol_quantity_list, periodic_table_dict): """Compute and return the total number of protons in all the elements listed in symbol_quantity_list. Parameters symbol_quantity_list is a compound list returned from the parse_formula function. Each small list in symbol_quantity_list has this form: ["symbol", quantity]. periodic_table_dict is the compound dictionary returned from make_periodic_table. Return: the total number of protons of all the elements in symbol_quantity_list. """sum_protonsfunction from yourmainfunction and print the number of protons for the user to see with the other output. 
Submission
To submit your program, return to I‑Learn and do these two things:
- Upload your
chemistry.pyfile for feedback. - Add a submission comment that specifies the grading category that best describes your program along with a one or two sentence justification for your choice. The grading criteria are:
- Some attempt made
 - Developing but significantly deficient
 - Slightly deficient
 - Meets requirements
 - Exceeds requirements