954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Pythagorean Theorem Calculator

By Scuppery on Jun 19th, 2008 5:47 am

If you are on school and you are taking algebra like me or just want to find the missing length of one side of the triangle than this program should help you out. Pythagorean Theorem Calculator is great and extremely essay to use thanks to this little program I pass my final exam.

#! /usr/bin/env python

#This program was made so for public use and for educational purposess.
#THIS PART IS NOT THE CODE ITSELF THESE ARE JUST COMMANDS
def menu():
    #print the options you have
    print "Welcome to Pythagorean Theorem Calculator.py"
    print "Please keep in mind that for this program to work you need the lenght of two side of the triangle"
    print " "
    print "Your Options Are:"
    print " "
    print "1) If you have the lenght of A and B"
    print " "
    print "2) If you have the lenght of C and A"
    print " "
    print "3) If you have the lenght of C and B"
    print " "
    print "4) Quit Pythagorean Theorem Calculator.py"
    print " "
    return input ("Choose your option: ")

from math import *

#On this one we have the lenght of A and B and we are trying to find the lenght of C
def AnB(a,b):
    print a**2, "+", b**2, "=", a**2 + b**2
    print sqrt(a**2 + b**2)

#On this one we have the lenght of A and C and we are trying to find the lenght of B
def CnA(c,a):
    print c**2, "-", a**2, "=", c**2 - a**2
    print sqrt(c**2 - a**2)

#On this one we have the lenght of B and C and we are trying to find the lenght of A
def CnB(c,b):
    print c**2, "-", b**2, "=", c**2 - b**2
    print sqrt(c**2 - b**2)

# NOW THE PROGRAM REALLY STARTS, AS CODE IS RUN
loop = 1
choice = 0
while loop == 1:
    choice = menu()
    if choice == 1:
        AnB(input("A: "),input("B: "))
    elif choice == 2:
        CnA(input("C: "),input("A: "))
    elif choice == 3:
        CnB(input("C: "),input("B: "))
    elif choice == 4:
        loop = 0

print "Thankyou fo using Pythagorean Theorem Calculator.py"

#END OF THE PROGRAM

Why did you import math near the middle of the code instead of the beginning?

AceofSpades19
Junior Poster in Training
61 posts since Jun 2008
Reputation Points: 61
Solved Threads: 10
 

Good question and I am glad you asked. Ok before I go on to explaining why I used there you must know that import math * could have been used anywhere in the code before the three functions(AnB, CnA, CnB). Now the reason why I used there, ok if you look at the code you will notice that everything before import math * statement is the menu of the program and like every program it needs to have commands to do things so I decided to put the import math * statement next to the three functions so the code is a bit neater. Most people use it on the beginning but it can also be used in other places and it become handy if you are writing a program out of the blue and don’t want to go all the way to the beginning to just type in three words. Hopefully that answer your question.

Scuppery
Newbie Poster
24 posts since Jun 2008
Reputation Points: 39
Solved Threads: 1
 

The recommended coding style for Python is:
Do your imports
Define your functions and/or classes
Do the main code

The bare minimum:
Define your functions before you use them in your main code

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

Very true that’s the recommended style for coding in python. However, as you start to learn a programming language you will see that many things can change and one of those is the style in which you code the programs. Thus, once you have learn the language you can give it your own little spice even thought that’s not what it is recommended. The point is that it doesn’t really matter where you do your imports as long as they work.

Scuppery
Newbie Poster
24 posts since Jun 2008
Reputation Points: 39
Solved Threads: 1
 

"\n" is your friend.

teddies
Newbie Poster
8 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
 

Write a program that will take a 3 digit number as an input, it will save the number in reverse order into a new integer variable.

Editor:
Open your own thread if you have a homework question!

saadbhatti
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

Sorry, I didn't see how old this was... my bad :D

hondros
Junior Poster
147 posts since Nov 2009
Reputation Points: 35
Solved Threads: 18
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You