# tempplus.py # Walker M. White (wmw2) # September 6, 2012 """Conversion functions between fahrentheit and centrigrade This module has been extended from the version in the previous lecture to include script code that test the functions. In addition, it shows the prefered way to construct an script: using a testing function.""" import cornelltest # Functions def to_centigrade(x): """Returns: x converted to centigrade Value returned has type float. Precondition: x is a float measuring temperature in fahrenheit""" return 5*(x-32)/9.0 def to_fahrenheit(x): """Returns: x converted to fahrenheit Value returned has type float. Precondition: x is a float measuring temperature in centigrade""" return 9*x/5.0+32 # Constants FREEZING_C = 0.0 # Temperature water freezes in centrigrade FREEZING_F = to_fahrenheit(FREEZING_C) BOILING_C = 100.0 # Temperature water freezes in centrigrade BOILING_F = to_fahrenheit(BOILING_C) # Test Procedure def test_module(): """Procedure to test out the conversion functions""" # Test to_centigrade cornelltest.assert_floats_equal(0.0,to_centigrade(32.0)) cornelltest.assert_floats_equal(100.0,to_centigrade(212.0)) cornelltest.assert_floats_equal(-40.0,to_centigrade(-40.0)) # Test to_fahrenheit cornelltest.assert_floats_equal(32.0,to_fahrenheit(0.0)) cornelltest.assert_floats_equal(212.0,to_fahrenheit(100.0)) cornelltest.assert_floats_equal(-40.0,to_fahrenheit(-40.0)) print 'The temperature module is working properly' # Script code if __name__ == '__main__': test_module()