Assignment 4
Assignment 4
Stage 1: We'll start by defining the function hypotenuse with two parameters, a and b, which
represent the lengths of the two legs of a right triangle. We'll use the Pythagorean theorem to
calculate the length of the hypotenuse (c).
return c
Stage 2: In this stage, we'll implement the Pythagorean Theorem to calculate the length of the
hypotenuse and return the result.
import math
c = math.sqrt(a**2 + b**2)
return c
Stage 3: Now, we'll test the function with the provided inputs and record the outputs.
result1 = hypotenuse(3, 4)
Output:
The hypotenuse of a right triangle with legs 3 and 4 is: 5.0
In Part 1, we have successfully developed the hypotenuse function incrementally, tested it with
different inputs.
Now, let's move on to Part 2, where I will create my own function using incremental
development.
Stage 1: Define a custom function. For example, let's create a function called calculate_discount
that calculates the discounted price of a product given its original price and a discount
percentage.
return discounted_price
Stage 2: Implement the calculation of the discounted price and return the result.
return discounted_price
Stage 3: Test the function with different inputs and record the outputs.
# Testing the calculate_discount function
Output:
In Part 2, we have successfully created and documented the calculate_discount function using
incremental development, tested it with different inputs, and recorded the outputs.
Reference
Downey, A. (2015). Think Python: How to think like a computer scientist. Needham,
Massachusetts: Green Tree
Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf