0% found this document useful (0 votes)
13 views

Assignment 4

The document describes incrementally developing two functions: 1) A hypotenuse function that calculates the length of the hypotenuse of a right triangle using the Pythagorean theorem. It defines the function, implements the calculation, and tests it with examples. 2) A calculate_discount function that calculates the discounted price given the original price and discount percentage. It follows the same incremental development process of defining, implementing, and testing the function.

Uploaded by

xpayne4
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)
13 views

Assignment 4

The document describes incrementally developing two functions: 1) A hypotenuse function that calculates the length of the hypotenuse of a right triangle using the Pythagorean theorem. It defines the function, implements the calculation, and tests it with examples. 2) A calculate_discount function that calculates the discounted price given the original price and discount percentage. It follows the same incremental development process of defining, implementing, and testing the function.

Uploaded by

xpayne4
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
You are on page 1/ 3

Part 1: Developing the hypotenuse Function

Stage 1: We'll start by defining the function hypotenuse with two parameters, a and b, which
represent the lengths of the two legs of a right triangle. We'll use the Pythagorean theorem to
calculate the length of the hypotenuse (c).

def hypotenuse(a, b):

c = 0 # Placeholder for now

return c

Stage 2: In this stage, we'll implement the Pythagorean Theorem to calculate the length of the
hypotenuse and return the result.

import math

def hypotenuse(a, b):

c = math.sqrt(a**2 + b**2)

return c

Stage 3: Now, we'll test the function with the provided inputs and record the outputs.

# Testing the hypotenuse function

result1 = hypotenuse(3, 4)

print(f"The hypotenuse of a right triangle with legs 3 and 4 is: {result1}")

result2 = hypotenuse(5, 12)

result3 = hypotenuse(8, 15)

print(f"The hypotenuse of a right triangle with legs 5 and 12 is: {result2}")

print(f"The hypotenuse of a right triangle with legs 8 and 15 is: {result3}")

Output:
The hypotenuse of a right triangle with legs 3 and 4 is: 5.0

The hypotenuse of a right triangle with legs 5 and 12 is: 13.0

The hypotenuse of a right triangle with legs 8 and 15 is: 17.0

In Part 1, we have successfully developed the hypotenuse function incrementally, tested it with
different inputs.

Now, let's move on to Part 2, where I will create my own function using incremental
development.

Part 2: Skilled and Versatile Software Developer.

Stage 1: Define a custom function. For example, let's create a function called calculate_discount
that calculates the discounted price of a product given its original price and a discount
percentage.

def calculate_discount(original_price, discount_percentage):

discounted_price = 0 # Placeholder for now

return discounted_price

Stage 2: Implement the calculation of the discounted price and return the result.

def calculate_discount(original_price, discount_percentage):

discounted_price = original_price - (original_price * discount_percentage / 100)

return discounted_price

Stage 3: Test the function with different inputs and record the outputs.
# Testing the calculate_discount function

result1 = calculate_discount(100, 20)

result2 = calculate_discount(50, 10)

result3 = calculate_discount(75, 15)

print(f"The discounted price is: ${result1}")

print(f"The discounted price is: ${result2}")

print(f"The discounted price is: ${result3}")

Output:

The discounted price is: $80.0

The discounted price is: $45.0

The discounted price is: $63.75

In Part 2, we have successfully created and documented the calculate_discount function using
incremental development, tested it with different inputs, and recorded the outputs.

Reference

Downey, A. (2015). Think Python: How to think like a computer scientist. Needham,
Massachusetts: Green Tree
Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf

You might also like