# You must make sure to run all cells in sequence using shift + enter or you might encounter errors
from pykubegrader.initialize import initialize_assignment
responses = initialize_assignment("12_precision_q", "week_2", "readings", assignment_points = 10.0, assignment_tag = 'week2-readings')
# Initialize Otter
import otter
grader = otter.Notebook("12_precision_q.ipynb")
โ Precision in Python: NIST Laboratory Adventures ๐งช#
Welcome to the National Institute of Standards and Technology (NIST) virtual laboratory! Today, weโll explore how Python handles numerical precision - a critical aspect of scientific measurements and calculations.
Could you imagine a world where a gallon of gasoline is not actually a gallon? Or a kilogram of sugar is not a kilogram? That would be a disaster! In the world of scientific computing, precision is equally important.
NIST is a world-renowned research institution that specializes in measurement science. They have developed a set of test cases to evaluate the numerical precision of programming languages. In this notebook, we will explore the NIST precision test cases and see how Python performs. For example, the meter has a physical reference standard at NIST. โ
NIST Meter#
# Run this block of code by pressing Shift + Enter to display the question
from questions._12_precision_q import Question1
Question1().show()
# Run this block of code by pressing Shift + Enter to display the question
from questions._12_precision_q import Question2
Question2().show()
# Run this block of code by pressing Shift + Enter to display the question
from questions._12_precision_q import Question3
Question3().show()
NIST Mass Calculation#
Instructions:#
A NIST scientist needs to calculate the average mass of several calibration weights and determine if it matches a reference value within an acceptable tolerance.
Define a list of masses
mass list
in grams: [10.0, 10.01, 9.001] make sure that all values are of type float.Use the built in
len()
function to calculate the number of weights. Store the results in the variablenumber_of_weights
. This parameter should be an integer.Calculate the average mass of the weights and store the result in the variable
average_mass
. You can use thesum()
andlen()
functions to calculate the average.Use the
math.isclose()
function to compare theaverage_mass
to the reference value10.0
with a relative tolerance of1e-3
. Store the result in the variableis_close
. You can set therel_tol
parameter to1e-3
. This is done by calling the functionmath.isclose(average_mass, 10.0, rel_tol=1e-3)
.
import math
# Step 1: Define the list of masses
mass_list = ...
# Step 2: Calculate the number of weights
number_of_weights = ...
# Step 3: Calculate the average mass
average_mass = ...
# Step 4: Compare average mass to the reference value
reference_value = ...
tolerance = ...
is_close = ...
# Step 5: Print the results
# We have provided the code to print the results
# Print results
print(f"Mass List: {mass_list}")
print(f"Number of Weights: {number_of_weights}")
print(f"Average Mass: {average_mass}")
print(f"Is Close to Reference: {is_close}")
grader.check("NIST-Mass-Calculation")
Submitting Assignment#
Please run the following block of code using shift + enter
to submit your assignment, you should see your score.
from pykubegrader.submit.submit_assignment import submit_assignment
submit_assignment("week2-readings", "12_precision_q")