Part 1
Incremental Development of a Hypotenuse Function
In software development, incremental development involves building a program step by step,
testing each stage to quickly identify and fix errors. According to Think Python, this
approach improves debugging efficiency and code reliability.
Function Skeleton
At this stage, we define the function without implementing the logic.
def hypotenuse(a, b):
pass
Explanation
This establishes the function structure. No computation occurs yet, so no output is expected.
Basic Output for Testing
We add a simple print statement to confirm the function runs correctly.
def hypotenuse(a, b):
print("Function is running")
hypotenuse(3, 4)
Output - Function is running
Explanation
This verifies that the function is callable and accepts parameters correctly.
Implement the Formula
We now apply the Pythagorean Theorem:
c=a2+b2
import math
def hypotenuse(a, b):
result = [Link](a**2 + b**2)
print(result)
hypotenuse(3, 4)
result = [Link](a**2 + b**2)
print(result)
hypotenuse(3, 4)
Output - 5.0
Explanation
The function computes the square root of the sum of squares. However, it only prints the
result instead of returning it.
Return the Value
import math
def hypotenuse(a, b):
# Computes hypotenuse using Pythagorean theorem
return [Link](a**2 + b**2)
print(hypotenuse(3, 4))
Output - 5.0
Explanation
The function now returns the computed value, making it reusable in other parts of the
program.
Additional Test Cases
print(hypotenuse(5, 12))
print(hypotenuse(8, 15))
Output - 13.0 / 17.0
Explanation
hypotenuse(5,12) → √(25 + 144) = √169 = 13
hypotenuse(8,15) → √(64 + 225) = √289 = 17
These outputs confirm correctness for different inputs.
.
Part 2
Custom Function Development (Portfolio Project)
To demonstrate programming skills, I developed a function that calculates the average and
grade classification of a student.
Function Definition
def student_grade(scores):
pass
Test Execution
def student_grade(scores):
print("Processing scores")
student_grade([80, 90, 70])
Output - Processing scores
Compute Average
def student_grade(scores):
avg = sum(scores) / len(scores)
print(avg)
student_grade([80, 90, 70])
Output - 80.0
Explanation
The function calculates the mean using sum() and len().
Add Grade Classification
def student_grade(scores):
avg = sum(scores) / len(scores)
# Determine grade classification
if avg >= 80:
grade = "A"
elif avg >= 70:
grade = "B"
elif avg >= 60:
grade = "C"
else:
grade = "D"
return avg, grade
print(student_grade([80, 90, 70]))
print(student_grade([60, 65, 58]))
print(student_grade([90, 95, 92]))
Outputs
(80.0, 'A')
(61.0, 'C')
(92.33, 'A')
Explanation
The function takes a list of scores as input (precondition, list must not be empty).
It computes the average using arithmetic operations.
It applies conditional logic to classify performance.
The postcondition is a tuple containing the average and grade.
This function demonstrates problem-solving, use of built-in functions, and conditional
statements. Incremental development ensured that each component was tested independently,
reducing debugging complexity and improving accuracy.
Reference
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea Press.
[Link]
Python Software Foundation. (2024). Python documentation. [Link]
W3Schools. (2024). Python functions.
[Link]