## Introduction: Python Program - Hypotenuse Using Pythagorean Theorem

Simple Python program using functions to calculate the hypotenuse of a triangle using the Pythagorean Theorem.

Attached as .py file and PDF file.

# This program will take 2 numbers from the user and # find the hypotenuse using the Pythagorean theorem import math # Function to square the numbers def squarefunction(length): square = length * length print "The square of a side is: ", square return square # end of function # Function to calculate Pythagorean theorem def pythagorean(aside, bside): HypotenuseSquared = aside + bside hypotenuse = math.sqrt(HypotenuseSquared) print "The hypotenuse of the 2 sides is: ", hypotenuse # end of function # Get the length of the sides from the user firstside = input("Enter the first side: ") secondside = input("Enter the second side: ") # Get the squares of 2 sides firstsidesquared = squarefunction(firstside) secondsidesquared = squarefunction(secondside) # print "The firstside variable is: ", firstside # print "The secondside variable is: ", secondside # Put the squares into the Pythagorean function pythagorean(firstsidesquared, secondsidesquared)

## Step 1: Code

# This program will take 2 numbers from the user and # find the hypotenuse using the Pythagorean theorem import math # Function to square the numbers def squarefunction(length): square = length * length print "The square of a side is: ", square return square # end of function # Function to calculate Pythagorean theorem def pythagorean(aside, bside): HypotenuseSquared = aside + bside hypotenuse = math.sqrt(HypotenuseSquared) print "The hypotenuse of the 2 sides is: ", hypotenuse # end of function # Get the length of the sides from the user firstside = input("Enter the first side: ") secondside = input("Enter the second side: ") # Get the squares of 2 sides firstsidesquared = squarefunction(firstside) secondsidesquared = squarefunction(secondside) # print "The firstside variable is: ", firstside # print "The secondside variable is: ", secondside # Put the squares into the Pythagorean function pythagorean(firstsidesquared, secondsidesquared)

## 4 Comments

6 years ago

Don't you need to take the square root of a^2 + b^2 ton find the hypotenuse

Reply 6 years ago

Thanks for the heads up! Made the change. If you have a moment, can you QA the code?

Reply 6 years ago

You are right! Thank you. Will correct it.

Matt

6 years ago

Ok, made the change. Thank you!