0% found this document useful (0 votes)
4 views6 pages

Programming Assignment Unit 4

The document outlines a programming assignment focused on developing two functions: one to calculate the hypotenuse of a right triangle using the Pythagorean theorem and another to compute Body Mass Index (BMI). It emphasizes incremental development, where each function is built step by step, tested, and debugged at each stage. The assignment reinforces key programming concepts such as function definition, arithmetic operations, and testing while maintaining clean code.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Programming Assignment Unit 4

The document outlines a programming assignment focused on developing two functions: one to calculate the hypotenuse of a right triangle using the Pythagorean theorem and another to compute Body Mass Index (BMI). It emphasizes incremental development, where each function is built step by step, tested, and debugged at each stage. The assignment reinforces key programming concepts such as function definition, arithmetic operations, and testing while maintaining clean code.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Programming Assignment: Unit 4

Part 1: Hypotenuse Function (Incremental Development)

Stage 1: Understanding the problem

The client wants a function to calculate the length of the hypotenuse of a right triangle.

According to the Pythagorean Theorem, the hypotenuse can be calculated using:

hypotenuse = sqrt(a^2 + b^2)

Where a and b are the lengths of the other two sides.

Stage 2: Basic structure of the function

We start by writing a function hypotenuse(a, b) without the final computation. We’ll use

print to inspect values.

import math

def hypotenuse(a, b):

print("a:", a)

print("b:", b)

hypotenuse(3, 4)

Output:
Stage 3: Adding intermediate computation

Now we compute squares of a and b.

def hypotenuse(a, b):

a_squared = a ** 2

b_squared = b ** 2

print("a^2:", a_squared)

print("b^2:", b_squared)

hypotenuse(3, 4)

Output:

Stage 4: Adding final computation and return

Now we compute the sum and return the square root.

def hypotenuse(a, b):

a_squared = a ** 2

b_squared = b ** 2
sum_squares = a_squared + b_squared

result = [Link](sum_squares)

return result

Stage 5: Testing the function

print("hypotenuse(3, 4) =", hypotenuse(3, 4))

print("hypotenuse(5, 12) =", hypotenuse(5, 12))

print("hypotenuse(8, 15) =", hypotenuse(8, 15))

Output:

Part 2: Custom Function - Body Mass Index Calculator

Stage 1: Understanding the problem

I want to create a function bmi(weight, height) that computes a person’s Body Mass Index

using the formula:

BMI = weight / height^2 (weight in kg, height in meters)

Stage 2: Skeleton function with input

def bmi(weight, height):

print("Weight:", weight)

print("Height:", height)
bmi(70, 1.75)

Output:

Stage 3: Adding calculation

def bmi(weight, height):

height_squared = height ** 2

print("Height squared:", height_squared)

bmi_value = weight / height_squared

print("BMI:", bmi_value)

bmi(70, 1.75)

Output:

Stage 4: Final function with return value

def bmi(weight, height):

height_squared = height ** 2
bmi_value = weight / height_squared

return round(bmi_value, 2)

Stage 5: Testing the function

print("bmi(70, 1.75) =", bmi(70, 1.75))

print("bmi(90, 1.80) =", bmi(90, 1.80))

print("bmi(50, 1.60) =", bmi(50, 1.60))

Output:

Descriptive Part

This assignment demonstrated the principle of incremental development, which involves

building a function step by step, testing and debugging at each stage. In Part 1, I developed

a hypotenuse function by first printing inputs, then computing squares, and finally

calculating the square root of their sum using [Link](). Testing with standard triangle

values such as (3, 4), (5, 12), and (8, 15) confirmed correct outputs (5.0, 13.0, and 17.0,

respectively).

In Part 2, I showcased a custom function bmi(weight, height) that calculates Body Mass

Index, which is useful in health-related applications. I followed the same incremental

pattern: starting with input display, then performing intermediate computation (height
squared), and finally implementing and returning the final result rounded to two decimal

places. The outputs matched expected BMI values, verifying function accuracy.

Incremental development simplifies debugging by narrowing down the possible sources of

error at each stage. This assignment helped reinforce core programming concepts such as

function definition, return values, arithmetic operations, and testing, all while maintaining

clean and readable code.

References:

 Downey, A. (2015). Think Python: How to Think Like a Computer Scientist. Green

Tea Press. [Link]

You might also like