In [19]:
assert True, 'This is an assert statement'
# the code will only run if the criteria is true
In [3]:
num = 2.003
In [4]:
assert num> 2, 'value must be > 2'
In [6]:
assert num < 2.001, 'value can only be a bt bigger than 2'
In [9]:
# error message that the answer doesn't fit the criteria it should
# assert statements are good when you need to create a function, i.e a funcion only works with positive numbers
# another common place is at the end of a function, i.e check that the answer is right eg. answer in kelvin is negative
In [16]:
def 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, 'Kelvin must be positive'
return temp_in_K
In [17]:
print(C_to_K(10))
In [18]:
print(C_to_K(-280))
In [1]:
#another note
In [ ]: