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 very large, fixed 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:
- Sum 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 into the 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 mass of the sample of glucose by the molar mass of glucose which results in 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
previous lesson’s prove milestone. When you are finished with this
prove assignment, your chemistry.py
program must
contain at least three functions named as follows:
main
make_periodic_table
compute_molar_mass
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
, andapprox
to automatically verify that functions are correct. It also contains an example test function and links to additional documentation aboutpytest
.
Help from a 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.py
Python file and save it in the same folder where you saved yourchemistry.py
program from the previous prove milestone. Theformula.py
file includes aFormulaError
class and a function namedparse_formula
. Both of them are complete and work correctly, and you should not change them. - Open the
formula.py
file in VS Code and read the triple quoted string at the top of theparse_formula
function. As the triple quoted string states, theparse_formula
function 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.py
program at the top of your program. This statement will import theparse_formula
function from theformula.py
file into yourchemistry.py
program so that you can call theparse_formula
function in your program.from formula import parse_formula
- In your
chemistry.py
program, change the compound list that is in yourmake_periodic_table
function to a compound dictionary. Each item in the dictionary should have a chemical symbol as the key and the chemical name and atomic mass in a list as the value, 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.py
program. Be certain not to 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. return
compute_molar_mass
. Read the docstring and comments in thecompute_molar_mass
function and write the code for that function. Note that you can complete thecompute_molar_mass
function by writing ten or fewer lines of code. - Modify the
main
function in yourchemistry.py
program 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.py
program by calling themain
function. - While executing the
main
function, the computer calls theinput
andfloat
functions. - While still executing the
main
function, the computer calls themake_periodic_table
,parse_formula
, andcompute_molar_mass
functions. - Finally, while still executing the
main
function, the computer calls theprint
function.
Testing Procedure
Verify that your program works correctly by following each step in this testing procedure:
- Download the
test_chemistry_2.py
Python file and save it in the same folder where you saved yourchemistry.py
program. Run thetest_chemistry_2.py
file 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.py
program. Read the output frompytest
, fix the mistake, and run thetest_chemistry_2.py
file 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.py
program. 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_name
with 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_name
function from yourmain
function 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_protons
with 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_protons
function from yourmain
function 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.py
file 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