Programming assignment unit 4
Programming assignment unit 4
the length of the hypotenuse of a right-angled triangle, given the lengths of the two legs.
We use incremental development, a process that involves building and testing the
pass
At this stage, the function accepts two inputs but performs no calculations. This sets up
a_squared = a ** 2
b_squared = b ** 2
print(a_squared, b_squared)
Test input:
hypotenuse(3, 4)
Output:
9 16
This confirms that the squaring step works correctly.
a_squared = a ** 2
b_squared = b ** 2
print(sum_squares)
Test input:
hypotenuse(3, 4)
Output:
25
Now we verify that the function correctly calculates the sum of the squares.
We finalize the function by computing the square root of the sum of squares:
import math
return math.sqrt(a ** 2 + b ** 2)
Test Outputs
print(hypotenuse(3, 4))
print(hypotenuse(5, 12))
print(hypotenuse(8, 15))
These results are consistent with known Pythagorean triples, confirming the function
works correctly.
Conclusion:
logic is validated. This approach leads to robust and maintainable code, especially in
Calculator
created a custom function that calculates compound interest. The function demonstrates
t
A=P×(1+r )
Where:
pass
This establishes the structure of the function. No output is expected at this point — it’s
Next, we compute the compound interest using the standard formula. At this stage, we
amount = p * ((1 + r) ** t)
print(amount)
Test Input:
compound_interest(1000, 0.05, 2)
Output:
1102.5
Instead of printing, the function should return a properly rounded result for practical use:
These test cases cover various principal amounts, interest rates, and time spans, and
Technical Explanation
This mirrors professional software development practices, where features are added
incrementally and tested immediately to catch bugs early and ensure correctness.
Reference:
Downey, A. (2015). Think Python: How to Think Like a Computer Scientist. Green Tea
Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf