# ex5PhillConrad.py Exercise 5 for CS5nm, Fall 2008, Phill Conrad, UCSB # define several functions, and test them # First, we define a function that runs our test cases # Note that \ character at the end of a line means that the line continues # on the next line def check_expect(test,check,expect): if (check == expect): print "Test " + test + " passed." else: print "Test " + test + " failed: expected " + str(expect) + \ " but I got " + str(check) # Next define a function for the perimeter of a rectangle # consumes: length, width # produces: perimeter of the rectangle def perimRect(length,width): return 2 * length + 2 * width # Next, here are some test cases for perimRect: check_expect("perimRect test 1", perimRect(2, 3), 10) check_expect("perimRect test 2", perimRect(4, 2.5), 13) check_expect("perimRect test 3", perimRect(3, 3), 12) # Next define a function to convert Fahrenheit to Celsius # consumes: temperature in Fahrenheit # produces: temperature in Celsius def FtoC(fTemp): return fTemp - 32.0 / 9.0 * 5.0 # Next, here are some test cases for FtoC check_expect("FtoC test 1", FtoC(212), 100) check_expect("FtoC test 2", FtoC(32), 0) check_expect("FtoC test 3", FtoC(68), 20) # Next define a function for the area of a rectangle # consumes: length, width # produces: perimeter of the rectangle def areaRect(length,width): return -1 # stub check_expect("areaRect test 1", areaRect(2, 3), 6) check_expect("areaRect test 2", areaRect(4, 2.5), 10) check_expect("areaRect test 3", areaRect(3, 3), 9)