04-assert


In [1]:
# Use an assert in function at the begining and end of the function
# The programmer should know what data types and values are needed at the begining and
# Return value of the function.
# Preconversion and Postconversions
assert True, "This is an assert statement"

In [2]:
num = 2.003

In [3]:
assert num > 2, "This value must be > 2"

In [4]:
assert num < 2.001, "This value must be < 2"


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-4-21b9428e56a4> in <module>()
----> 1 assert num < 2.001, "This value must be < 2"

AssertionError: This value must be < 2

In [5]:
def temp_c_to_k(temp_in_c):
    
    """Convert temperature from C to K
    """
    temp_in_k = temp_in_c + 273.15
    assert temp_in_k > 0, "K must be positive"
    return temp_in_k

In [13]:
print (temp_c_to_k(-274.15))


-1.0

In [ ]: