0% found this document useful (0 votes)
27 views9 pages

Assignment Unit 4

The document outlines a programming assignment focused on creating two functions using incremental development: one for calculating the hypotenuse of a right triangle and another for calculating the final price of a product after applying a discount and tax. Each function is built step-by-step, ensuring accuracy and reliability through testing at each stage. The assignment emphasizes the importance of structured programming and real-world applications in commerce.

Uploaded by

beheshtasakhaye
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)
27 views9 pages

Assignment Unit 4

The document outlines a programming assignment focused on creating two functions using incremental development: one for calculating the hypotenuse of a right triangle and another for calculating the final price of a product after applying a discount and tax. Each function is built step-by-step, ensuring accuracy and reliability through testing at each stage. The assignment emphasizes the importance of structured programming and real-world applications in commerce.

Uploaded by

beheshtasakhaye
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

1

Programming Assignment Unit 4

UNIVERSITY OF THE PEOPLE

CS 1101 - Programming Fundamentals

Mike Augustine

May 08, 2025


2

Unit 4: Functions and Return Values

Part 1:

Project: Calculator using Incremental Development

Goal: Create a function hypotenuse(a, b) that calculates the length of the hypotenuse of a

right triangle using the Pythagorean theorem: a 2+ b2=c 2

Stage 1: Set up the function structure

Explanation:

We begin by defining the function and verifying that it accepts two parameters. For

now, it simply returns a placeholder value to confirm that the function runs.

Code:

def hypotenuse(a, b):

return 0 # placeholder

Test:

print(hypotenuse(3, 4)) # Expected output: 0 (for test only)

Output: 0

Stage 2: Calculate the squares of a and b

Explanation:

Now we calculate a 2andb 2 and return their sum. This verifies that squaring and

addition work as expected before introducing the square root function.

Code:

def hypotenuse(a, b):


3

square_sum = a**2 + b**2

return square_sum

Test:

print(hypotenuse(3, 4)) # Expected output: 25

Output: 25

Stage 3: Take the square root

Explanation:

With the sum correctly calculated, we now apply the square root using Python's math

module.

Code:

import math

def hypotenuse(a, b):

square_sum = a**2 + b**2

result = math.sqrt(square_sum)

return result

Test:

print(hypotenuse(3, 4)) # Expected output: 5.0

Output: 5.0

Stage 4: Final Testing with Multiple Inputs

Now we verify the function with multiple sets of values:


4

Python code with output:

Summary

Through incremental development, we gradually built and tested each part of the function,

minimizing error risk and improving code reliability. The function hypotenuse(a, b) now

returns accurate hypotenuse lengths when given valid inputs.

Part 2:

Portfolio Function Using Incremental Development


5

Project Title: calculate_discounted_price – A Real-World Discount & Tax Calculator

Objective:

To build a custom Python function that calculates the final price of a product after applying a

discount and adding tax. This function simulates how prices are calculated in real-world

commerce, such as in retail stores or e-commerce systems. The function will take three

inputs:

1. original_price: The base price of the product.

2. discount_percentage: A percentage of discount to apply.

3. tax_percentage: A percentage of tax to add after applying the discount.

The function will be developed using incremental development, a programming technique

where the function is built and tested in small, manageable steps.

Stage 1: Define Function with Parameters

Goal: Define the function with appropriate parameters and verify that it runs.

Explanation:

At this point, the function is just a shell. It accepts the three required parameters but

does not yet perform any calculations. Returning a fixed value ensures the syntax and

function call structure are correct before implementing logic.

Code:

def calculate_discounted_price(original_price, discount_percentage, tax_percentage):

return 0 # Placeholder to test the structure

Test:
6

print(calculate_discounted_price(100, 10, 5)) # Expected: 0 (placeholder)

Output: 0

Conclusion: The function runs successfully, and we can proceed to implement the

discount logic.

Stage 2: Apply the Discount

Goal: Calculate and return the discounted price before applying tax.

Explanation:

We apply the discount by subtracting the discount percentage from 100%, then

multiplying the original price by the resulting fraction. For example, a 10% discount

on $100 results in:

100×(1−0.10)=90.0

Code:

def calculate_discounted_price(original_price, discount_percentage, tax_percentage):

discounted_price = original_price * (1 - discount_percentage / 100)

return discounted_price # Just testing discount logic

print(calculate_discounted_price(100, 10, 5)) # Expected: 0 (placeholder)

Test:

print(calculate_discounted_price(100, 10, 5)) # Expected: 90.0

Output: 90

Conclusion: The function now successfully calculates the discounted price. We will

now include the tax logic.


7

Stage 3: Add Tax Calculation

Goal: Calculate and return the final price after discount and tax.

Explanation:

After discounting, tax is added by multiplying the discounted price by a factor of (1 +

tax_percentage / 100). The round() function is used to limit the result to two decimal

places for better currency formatting.

Code:

def calculate_discounted_price(original_price, discount_percentage, tax_percentage):

discounted_price = original_price * (1 - discount_percentage / 100)

final_price = discounted_price * (1 + tax_percentage / 100)

return round(final_price, 2) # Round to 2 decimal places for currency format

Final Testing: Function Calls with Explanation

Python

code

with

output:
8

Summery:

Using incremental development allowed us to isolate potential issues and test each step of our

logic. This not only made debugging easier but ensured accurate results at every stage. This

method mimics how real-world software is developed in agile teams or solo freelance

projects.

This function is practical for a variety of domains, including:

 Online shopping platforms

 Point-of-sale systems

 Invoice generation tools

Additionally, using built-in functions like round() and percentage-based math showcases

knowledge of core programming concepts such as arithmetic operators, function design, and

modularity.

Word count: 735 words


9

References:

 Downey, A. (2015). Think Python: How to think like a computer scientist. Needham,

Massachusetts: Green Tree

https://greenteapress.com/thinkpython2/thinkpython2.pdf

 Software Foundation. (2023). Built-in Functions – round(). Python 3.11

documentation. https://docs.python.org/3/library/functions.html#round

 Python Software Foundation. (2023). math — Mathematical functions. Python 3.11.

https://docs.python.org/3/library/math.html

You might also like