Programming assignment
Full Name: Hassan Agha Hussainy
Part One:
First, we need to define a function that takes two arguments, which represent the lengths of the two legs
of a right triangle.
Next, we implement the Pythagorean theorem to calculate the hypotenuse. Finally, I test the function with
different inputs to ensure it works correctly.
Part two: Developing a Custom Function for a Portfolio
For this part of the assignment, let's create a function that calculates the area and perimeter of a
rectangle given its length and width. This will demonstrate our ability to develop custom software
solutions using an incremental development approach.
Stage 1: Define the Function Signature
First, we need to define the function signature. This includes the function name, parameters, and return
values.
Stage 2: Calculate the Area
Next, we calculate the area of the rectangle. The area of a rectangle is given by the formula: area = length
* width.
Test Input and Output:
Stage 3: Calculate the Perimeter
Now, we calculate the perimeter of the rectangle. The perimeter of a rectangle is given by the formula:
perimeter = 2 * (length + width).
Explanation:
Function Overview
This function calculates and returns two key properties of a rectangle: its area and perimeter.
Parameters
length: The length of the rectangle.
width: The width of the rectangle.
Calculations
1. Area Calculation:
- The area of a rectangle is calculated by multiplying its length by its width.
- In the function, this is represented as area = length * width.
2. Perimeter Calculation:
- The perimeter of a rectangle is the total distance around its edges, which can be calculated by
adding all sides together.
- Since a rectangle has two lengths and two widths, the formula is 2 * (length + width).
- In the function, this is represented as perimeter = 2 * (length + width).
Return Values
The function returns both the calculated area and perimeter as a tuple.
Test Input and Output:
Explanation
Function Call:
- rectangle_properties(5, 3) calls the rectangle_properties function with length = 5 and width = 3.
This function calculates the area and perimeter of a rectangle with these dimensions.
Assignment of Return Values:
- The function returns two values: area and perimeter.
- These values are unpacked and assigned to the variables area and perimeter on the left side of the
assignment operator (=).
- This is a Python feature called tuple unpacking, where multiple values returned by a function can
be directly assigned to multiple variables.
Printing the Results:
The print statement uses an f-string to format the output.
It displays the calculated area and perimeter with descriptive labels.
Step-by-Step Calculation
Given length = 5 and width = 3:
Area Calculation: area = length * width = 5 * 3 = 15
Perimeter Calculation: perimeter = 2 * (length + width) = 2 * (5 + 3) = 2 * 8 = 16
Stage 4: Handle Invalid Inputs
Finally, we add error handling to ensure that the inputs are valid numbers.
Explaination:
Function Definition
- def rectangle_properties(length, width):
o This line defines a function named rectangle_properties that takes two arguments: length
and width. These are intended to represent the dimensions of a rectangle.
Input Validation
- if not isinstance(length, (int, float)) or not isinstance(width, (int, float)):
o This line checks if either length or width is not an integer or a float.
o isinstance(variable, (type1, type2, ...)) is a Python function that checks if a variable is an
instance of a specified type (or types). In this case, it's checking if length and width are of
type int (integer) or float (floating-point number).
o raise ValueError("Both length and width must be number.")
o If the condition in the if statement is true (i.e., either length or width is not a number),
this line raises a ValueError exception. This signals that there's an issue with the input to
the function. The error message provides information about the problem. Note: as
mentioned before, the error message has a typo and should say "numbers."
- if length <= 0 or width <= 0:
o This line checks if either length or width is less than or equal to 0. Rectangles cannot
have non-positive dimensions, so this is another check for invalid input.
o raise ValueError("Both length and width must be positive.")
If the condition is true, this line raises a ValueError exception with a message indicating that the
dimensions must be positive.
Explanation
Function Call:
- rectangle_properties(7, 4) calls the rectangle_properties function with length = 7 and width = 4.
- This function calculates the area and perimeter of a rectangle with these dimensions.
Assignment of Return Values:
- The function returns two values: area and perimeter.
- These values are unpacked and assigned to the variables area and perimeter on the left side of the
assignment operator (=).
- This is a Python feature called tuple unpacking, where multiple values returned by a function can
be directly assigned to multiple variables.