Python Program - Hypotenuse Using Pythagorean Theorem

14,220

12

4

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)


Be the First to Share

    Recommendations

    • DIY Summer Camp Contest

      DIY Summer Camp Contest
    • Maps Challenge

      Maps Challenge
    • Fruit and Veggies Speed Challenge

      Fruit and Veggies Speed Challenge

    4 Comments

    0
    JohnK124
    JohnK124

    5 years ago

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

    0
    matt392
    matt392

    Reply 5 years ago

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

    0
    matt392
    matt392

    Reply 5 years ago

    You are right! Thank you. Will correct it.
    Matt

    0
    matt392
    matt392

    5 years ago

    Ok, made the change. Thank you!