# Tutorial 2 - Problem 3 # # Read Section 6.2 Incremental Development from our e-textbook # # The goal of incremental development is to ease development (it is easier to implement one feature at a time # than implementing a large number of them at a time) and avoid long debugging sessions. # The first step is to consider what a hypotenuse function should look like in Python. # In other words, what are the inputs (parameters) and what is the output (return value)? # # In this case, the inputs are two sides, which we can represent using two numbers. The # return value is the hypotenuse represented by a floating-point value. # Immediately we can write an outline of the function: # Incremental development 1 def hypotenuse(side1, side2): # For debugging purposes: print("side1 : %d, side2 : %d" %(side1, side2)) result = 0 return result # Main side1 = 4 side2 = 3 print("Calling hypotenuse(%d,%d) and the result is: %f." %(side1, side2, hypotenuse(side1, side2))) # Obviously, this version doesn’t compute hypotenuses: it always returns zero. But it is syntactically # correct, and it runs, which means that we can test it before we make it more complicated. # Note that we have added some some scaffolding print statements to ensure the parameters are received correctly. ------------------------------------------------------------------- # At this point we have confirmed that the function is syntactically correct, and we can start # adding code to the body. A reasonable next step is to compute the sum of squares: side1**2 + side2**2 # Let's add to our scaffolding print statement # TESTING: The chosen test case uses side1 = 4 and side2 = 3 as test data and the expected result is 25. # If the function is working, the program should display 25. If so, we know that the # function is getting the right parameters and performing the first computation correctly. # If not, there are only a few lines to check. # Incremental development 2 def hypotenuse(side1, side2): result = side1**2 + side2**2 # For debugging purposes: print("side1 : %d, side2 : %d, result : %f" %(side1, side2, result)) return result # Main side1 = 4 side2 = 3 print("Calling hypotenuse(%d,%d) and the result is: %f." %(side1, side2, hypotenuse(side1, side2))) ------------------------------------------------------------------- # Next we compute the hypotenuse by taking the square root of the sum of squares. # # TESTING: The chosen test case uses side1 = 4 and side2 = 3 as test data and the expected result is 5. # Incremental development 3 import math def hypotenuse(side1, side2): result = math.sqrt(side1**2 + side2**2) # For debugging purposes: print("side1 : %d, side2 : %d, result : %f" %(side1, side2, result)) return result # Main side1 = 4 side2 = 3 print("Calling hypotenuse(%d,%d) and the result is: %f." %(side1, side2, hypotenuse(side1, side2))) ------------------------------------------------------------------- # The final version of the function doesn’t display anything when it runs; it only returns a value. # The scaffolding print statements are useful for debugging, but once we get the # function working, we can remove them or comment them out. Code like that is called scaffolding because it # is helpful for building the program but is not part of the final product. # Incremental development 4 import math def hypotenuse(side1, side2): result = math.sqrt(side1**2 + side2**2) return result # Main side1 = int(input("Please, enter side 1: ")) side2 = int(input("Please, enter side 2: ")) print("Calling hypotenuse(%d,%d) and the result is: %f." %(side1, side2, hypotenuse(side1, side2))) ------------------------------------------------------------------- # Error handling - make sure that the values entered by the user are valid # Incremental development 5 import math def hypotenuse(side1, side2): result = math.sqrt(side1**2 + side2**2) return result # Main side1 = input("Please, enter side 1: ") side2 = input("Please, enter side 2: ") # Add guardian code here to make sure that the values entered by the user are valid # Make sure side1 and side2 are converted to "int" print("Calling hypotenuse(%d,%d) and the result is: %f." %(side1, side2, hypotenuse(side1, side2))) ------------------------------------------------------------------- # Summary: # When we start out, we should add only a line or two of code at a time. As we gain more # experience, we might find ourself writing and debugging bigger chunks. Either way, # incremental development can save us a lot of debugging time. # The key aspects of the process are: # 1. Start with a working program and make small incremental changes. At any point, if # there is an error, we should have a good idea where it is. # 2. Use variables to hold intermediate values so we can display and check them. # We did not do this in the above function. This is something we can do when the # computation is more complicated and can be divided into pieces. # 3. Once the program is working, we might want to remove some of the scaffolding or # consolidate multiple statements into compound expressions, but only if it does not # make the program difficult to read. -------------------------------------------------------------------