Unit 4: Programming Assignment: Functions and Return Values
BUS 3305-01 Business Law and Ethics - AY2025-T5
Part 1: Hypotenuse Function Using Incremental Development
Here, I will use incremental development to develop a function called hypotenuse that
calculates the hypotenuse of a right triangle based on the lengths of the other two sides.
Incremental development is a style allowing a programmer to test and ensure the correctness
of various parts of the code at different stages, thus reducing potential bugs and enhancing
clarity (Downey, 2015).
Stage 1: Function Setup and Input Check
Explanation:
In this step we confirm the inputs were received properly; no computation yet.
Output:
Stage 2: Compute Squares
The Pythagorean theorem formula
hypotenuse=√a2+b2
I shall use the ** operator for exponentiation and the Python’s math.sqrt for square root
Explanation:
In this version, the squares of both legs are computed and printed.
Output:
Explanation: We added the actual formula to calculate the hypotenus with a=3 and b=4 as
inputs to get the hypotenuse = 5.0.
We Test input hypotenuse (3,4), hypotenuse (5,12) and hypotenuse (7,24)
Conclusion
Having applied incremental development to create and test a hypotenuse calculator, we can
now use the same process to correctly determine and calculate the hypotenuse of a right angle
triangle
Part 2: Custom Function – Body Mass Index (BMI) Calculator
Introduction
To prove flexibility, I implemented a function to help with calculating BMI. BMI is
calculated as:
BMI=weight (kg)/height (m)2
Many healthcare applications can use this as an index to assess whether a patient's body mass
is a health issue or not.
Stage 1: Value checking
def bmi(weight, height):
print("====================================")
print(" BMI CALCULATOR SYSTEM ")
print("====================================")
print(f">> Weight entered : {weight} kg")
print(f">> Height entered : {height} m")
print("====================================")
Output:
====================================
BMI CALCULATOR SYSTEM
====================================
>> Weight entered : 70 kg
>> Height entered : 1.75 m
====================================
Stage 2: Computation of BMI
def bmi(weight, height):
print("====================================")
print(" BMI CALCULATOR SYSTEM ")
print("====================================")
print(f">> Weight entered : {weight} kg")
print(f">> Height entered : {height} m")
bmi_value = weight / (height ** 2)
print("------------------------------------")
print(f">> Calculated BMI : {bmi_value:.2f}")
print("====================================")
# Take it for a test run
bmi(70, 1.75)
Output:
====================================
BMI CALCULATOR SYSTEM
====================================
>> Weight entered : 70 kg
>> Height entered : 1.75 m
------------------------------------
>> Calculated BMI : 22.86
====================================
Stage 3: Categorization
def bmi(weight, height):
"""Calculate BMI and categorization based on standard WHO definitions."""
print("==============================================")
print(" BMI CALCULATOR SYSTEM v1.0 ")
print("==============================================")
print(f">> Weight entered : {weight} kg")
print(f">> Height entered : {height} m")
bmi_value = weight / (height ** 2)
if bmi_value < 18.5:
category = "Underweight"
elif 18.5 <= bmi_value < 25:
category = "Normal weight"
elif 25 <= bmi_value < 30:
category = "Overweight"
else:
category = "Obese"
print("----------------------------------------------")
print(f">> Calculated BMI : {bmi_value:.2f}")
print(f">> BMI Category : {category}")
print("==============================================\n")
return bmi_value, category
# Final Tests
bmi(70, 1.75)
bmi(50, 1.6)
bmi(95, 1.7)
Final Output:
==============================================
BMI CALCULATOR SYSTEM v1.0
==============================================
>> Weight entered : 70 kg
>> Height entered : 1.75 m
----------------------------------------------
>> Calculated BMI : 22.86
>> BMI Category : Normal weight
==============================================
==============================================
BMI CALCULATOR SYSTEM v1.0
==============================================
>> Weight entered : 50 kg
>> Height entered : 1.6 m
----------------------------------------------
>> Calculated BMI : 19.53
>> BMI Category : Normal weight
==============================================
==============================================
BMI CALCULATOR SYSTEM v1.0
==============================================
>> Weight entered : 95 kg
>> Height entered : 1.7 m
----------------------------------------------
>> Calculated BMI : 32.87
>> BMI Category : Obese
==============================================
The second was calculated with simple squares and the third finished the task from the
Pythagorean theorem. Each task has been confirmed as correct.
Technical Explanation and Reflection
With Part 1, the implementation of the hypotenuse function was done incrementally.
The first version simply ensured the input was entered correctly. The second computed the
square of each input. The third one completed the computation using the Pythagorean
theorem. Every step of the program was tested to ensure it was correct.
For Part 2, I implemented a BMI calculator function in three stages. In the first phase,
the function validated input. The second phase computed the BMI. In its final phase, the
function incorporates a logic to classify health based on the BMI value. Tests with different
arguments showed that the function adapts well to inputs and correctly categorizes outputs.
This method bears particular significance for real-world cases like health care. For
example, even one wrong logic or calculation step could mean quite harsh consequences. At
the same time, through clear structure, well-documented code, and testing, this solution
becomes an asset to the professional portfolio, demonstrating ability to solve problems as
well as technical correctness.
References
Downey, A. (2015). Think Python: How to think like a computer scientist (2nd ed.). Green
Tree Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf
World Health Organization (WHO). (n.d.). BMI classification. https://www.who.int/news-
room/fact-sheets/detail/obesity-and-overweight