Tag Archive : Pythagorean Theorem Professional Calculator -Python

Pythagorean Theorem Professional Calculator -Python

In this article, we have built a calculator that finds the length of any side of a right triangle (By Using Pythagorean Theorem) with the Python code. Please install python 3.8 from the link given below to run this code.
https://www.python.org/downloads/

I prefer you to use atom text editor to type your code or to save the given code:
https://atom.io/

You can download the the Pythagorean Theorem.py from GitHub using the given link:

Link for the code in GitHub

Code

#Pythagorean Theorem Professional Calculator -Python
def find_base(perpendicular,hypotenuse):
    base = ((hypotenuse**2)-(perpendicular**2))**(1/2)
    return base
#Function to find Base when Hypotenuse and perpendicular is given
def find_perpendicular(base,hypotenuse):
    perpendicular = ((hypotenuse**2)-(base**2))**(1/2)
    return perpendicular
#Function to find perpendicular when Hypotenuse and Base is given
def find_hypotenuse(base,perpendicular):
    hypotenuse= ((perpendicular**2)+(base**2))**(1/2)
    return hypotenuse
#Function to find Hypotenuse when perpendicular and Base is given
print("Enter any two values, Enter Unknown Value as '0'!!")
b = int(input('Base: '))
p = int(input('Perpendicular: '))
h = int(input('Hypotenuse: '))
#getting Input from user - Unknown Value is Entered '0'

if (h != 0) and (h<b or h<p): #CHECK: Whether Hypotenuse > Other 2 Sides
    print('Hypotenuse cannot be lesser than the other two sides')
elif (h == 0 and b == 0)or(b == 0 and p == 0)or(p == 0 and h == 0): #CHECK: Whether ONLY 1 side is Entered 0  or not
    print('Error Finding the value \n You Must Enter 2 Values \n Please Check And Try Again!')
elif (h == 0 or (h>b and h>p)) and (h == 0 or b == 0  or p == 0): # CHECK: If All Conditions Satisfy
    if b == 0:
        print(find_base(p,h)) #Using the Functions
    elif p == 0:
        print(find_perpendicular(b,h)) #Using the Functions
    elif h == 0:
        print(find_hypotenuse(b,p)) #Using the Functions
else:
    print("Error!") #If No Conditions Satisfy

Example Input:

Enter any two values, Enter Unknown Value as '0'!!
Base: 3
Perpendicular: 4
Hypotenuse: 0

Output:

5.0

Code Explanation:

In this code, what we basically do is that, we get the 3 inputs from the user which are the values of base, perpendicular and hypotenuse, where the unknown value is entered ‘0’.

Pythagorean Theorem

Function Definition:

In the first 12 lines we define three functions that finds the base, perpendicular and the hypotenuse [find_base , find_perpendicular, find_hypotenuse]

Input:

We Prompt 3 inputs from the user whose value is Base, Perpendicular and Hypotenuse respectively and the unknown value is entered ‘0’.

If…else:

The if statement checks if ‘hypotenuse’ is a known value, but is lesser than any of the two sides. If True , returns ‘Hypotenuse cannot be lesser than the other two sides’

The first elif statement checks if not more than one side is unknown so that the formula can be used. If true, returns:
‘Error Finding the value
You Must Enter 2 Values
Please Check And Try Again!’

The second elif statement simply returns the necessary output if any one value is unknown.

The else statement returns “Error!” if no conditions given satisfies

Conclusion:

The above article explains the Pythagorean Theorem Calculator using python coding. If you want to download the code from GitHub, click here. If you have any questions or feedback on this, feel free to post in the comments section below.