Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Python Math: Create a Pythagorean theorem calculator

Python Math: Exercise-68 with Solution

Write a Python program to create a Pythagorean theorem calculator.

Note : In mathematics, the Pythagorean theorem, also known as Pythagoras' theorem, is a fundamental relation in Euclidean geometry among the three sides of a right triangle. It states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides.

Sample Solution:-

Python Code:

from math import sqrt

print('Pythagorean theorem calculator! Calculate your triangle sides.')
print('Assume the sides are a, b, c and c is the hypotenuse (the side opposite the right angle')
formula = input('Which side (a, b, c) do you wish to calculate? side> ')

if formula == 'c':
	side_a = int(input('Input the length of side a: '))
	side_b = int(input('Input the length of side b: '))

	side_c = sqrt(side_a * side_a + side_b * side_b)
	
	print('The length of side c is: ' )
	print(side_c)

elif formula == 'a':
    side_b = int(input('Input the length of side b: '))
    side_c = int(input('Input the length of side c: '))
    
    side_a = sqrt((side_c * side_c) - (side_b * side_b))
    
    print('The length of side a is' )
    print(side_a)

elif formula == 'b':
    side_a = int(input('Input the length of side a: '))
    side_b = int(input('Input the length of side c: '))
        
    side_c = sqrt(side_c * side_c - side_a * side_a)
    
    print('The length of side b is')
    print(side_c)

else:
	print('Please select a side between a, b, c')
	

Sample Output:

Pythagorean theorem calculator! Calculate your triangle sides.                                                
Assume the sides are a, b, c and c is the hypotenuse (the side opposite the right angle                       
Which side (a, b, c) do you wish to calculate? side>a                                                         
Input the length of side b:10                                                                                 
Input the length of side c:15                                                                                 
The length of side a is                                                                                       
11.180339887498949 

Flowchart:

Flowchart: Create a Pythagorean theorem calculator

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to create a dot string.
Next: Write a Python function to round up a number to specified digits.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m