Programming Assignment Unit 3 by Zin Myo Tun (1)
Programming Assignment Unit 3 by Zin Myo Tun (1)
Contents
Recursive Countdown and countup3
Division by Zero Error Handling5
Works Cited............................................................................................................................................... 6
Part 1
You work as a software developer in a company that creates custom software solutions for various clients.
Your company has been approached by an educational client who wants to develop a function that
calculates the length of the hypotenuse of a right triangle given the lengths of the other two legs as
arguments. Your manager has instructed you to use incremental development to create the necessary
function and document each stage of the development process. After completing the final stage of
development, you have to test the function with different arguments and record the outputs in your
Learning Journal.
To calculate the length of the hypotenuse of a right triangle, I remember that the Pythagorean Theorem
must be utilized (a² + b² = c²), where:
a=height of the triangle; b=the length of the triangle; and c=the hypotenuse.
In the first stage, I define hypotenuse a & b using the code: def hypotenuse(a, b): return 0
In the second stage, I identify a² and b² so that the Python compiler knows how to calculate by using the
code:
def hypotenuse(a, b):
sqr_a = a ** 2
print(str(“square ‘a’ is: “ + sqr_a))
sqr_b = b ** 2
print(str(“square ‘b’ is: “ + sqr_b))
return 0
I use the exponentiation operator of ** to tell Python that this is an operation involving two numbers and
exponent of power, in our case, the power of 2, or squared. In the third stage, Iidentify ‘sqr_c’ and how I
am calculating for the result.
def hypotenuse(a, b):
a_sqr = a ** 2
b_sqr = b ** 2
c_sqr = a_sqr + b_sqr
print(“square c equals: “ + c_sqr)
return 0
In the final stage, I start with the command import math to ensure Python knows I am using this coding
for calculating a mathematical problem, then I re-enter my coding and add some input coding which will
prompt the user to enter values for ‘a’ & ‘b’ in order to calculate the hypotenuse and since I only want to
show the value of ‘c’, I need to enter a math function to calculate the square root of c² in order to just
show the length of the hypotenuse.
Part 1 of the assignment also asked us to show the output of two additional calls. For the first call, I used
a length of ’12’ and height of ‘17’ giving us an hypotenuse of ‘20’. For the second call, I used a much
larger triangle with a length of ‘140’ and height of ‘162’, calculating an hypotenuse of ‘214’. The
screenshot below displays the two additional calls
Part 2
You are a software developer who wants to establish yourself as a skilled and versatile programmer.
To achieve this, you have decided to create a work portfolio that showcases your ability to develop
custom software solutions.
This portfolio will be your gateway to attract potential clients and establish yourself as a freelancer.
As part of your portfolio, you plan to create your own function that does some useful computation using
an incremental development approach that will demonstrate your programming skills and problem-
solving abilities.
You will document each stage of the development process, including the code and any test input and
output in your Programming Assignment.
For part 2, I chose to calculate the area of a cube. The mathematical formula for finding the area of a cube
is 6 * Length of side².
I used ‘a’ to represent the lenght of a side.
For the first stage, I defined cube with the code:
def cube(a):
return 0
For the second stage, I needed to identify ‘a’, which is being used to identify the length of a side by
coding:
def cube(a):
cube = 6 * (a ** 2)
print(str(“Side of cube ‘a’ is: “))
return 0
In the third stage, I identified the area of the cube and identified a_sqr with this code:
def cube(a):
a_sqr = a ** 2
cube = 6 * a_sqr
print(‘Area of cube equals: “)
return 0
For the final stage, I import the math function, and re-enter my code and test my theory with this code:
def cube(a):
a_sqr = a ** 2
cube = 6 * a_sqr
return int(cube)
print(str(cube(2)))
As shown in the screenshot below, I entered ‘2’ as the length of my side which resulted in a result of ‘24’.
Downing, Allen (2015). Thinking Python: How to Think Like a Computer Scientist.
https://greenteapress.com/thinkpython2/thinkpython2.pdf