Imagine you work for a ramp building company which installs ramps over stairs in order for buildings to be ADA compliant. You will write a program to determine the length of the ramp based on the height of the stairs and the width of the ground. See the diagram below where a is the height and b is the width.
Figure: see image.
To write this code, you first need to review the Pythagorean theorem for right triangles. It allows us to find the length of c based on the length of a and b. First, make sure you understand the Pythagorean theorem:
c2 = a2 + b2
This tells us what c2 is. To find just c we would use:
c = square root of (a2 + b2)
Now, let's see an example of using that formula to find c if a = 3 and b = 4:
c = square root of (32 + 42)
c = square root of (9 + 16)
c = square root of 25
c = 5
To combine that into one line, it would be: c = square root (a squared + b squared)
Recall that the math module provides for square root and squaring:
math.sqrt(16) would result in 4
math.pow(3,2) is 32 which would result in 9.
Sample Execution:
Welcome to the ramp calculator.
Enter the height: 5
Enter the width: 12
The calculated ramp length is 13.0
Sample Execution:
Welcome to the ramp calculator.
Enter the height: 4.6
Enter the width: 7.8
The calculated ramp length is 9.055385138137417