# Geometry calculator
#  For calculating perimeter and area on various shapes
#
# Made by Ole Jensen @ http://dbhome.dk/learningtoprogram

# Importing the math mudule to get pi
import math

# Building the various functions
def square(s):
    """s is the length of any side"""
    per = s * 4
    area = s**2
    return per, area

def rect(b, h):
    """b is the base length, and h is the height"""
    per = (h * 2) + (b * 2)
    area = h * b
    return per, area

def circle(d):
    """d is the diameter of the circle"""
    r = d/2
    area = math.pi * r**2
    cir = d * math.pi
    return cir, area

def triangle(b, h):
    """b is the base length, and h is the height.\n\
The triangle needs to be a right triangle"""
    # in case numbers are integers:
    area = 0.5*b*h
    per = (math.sqrt(pow(b,2) + pow(h,2))) + h + b
    return per, area

# Starting with a nice wellcome
print "Wellcome to the area-and-perimeter program"
print "------------------------------------------"
print "\n"*3

# And a small menu so people can select what to do
print "What shape would you like calculated:"
print ""
# printing the menu:
print "1: Square"
print "2: Rectangle"
print "3: Circle"
print "4: Triangle"

# Let the user make his decison
choice = raw_input(">")

# And making sure he typed a correct number
while choice < 1 or choice > 4 or type(choice) != int:
    try:
        choice = int(choice)
    except:
        choice = raw_input("Please you should type the number to the left of you desired shape!\n>")

print ""
if choice == 1:
    print "SQUARE - []"
    side = raw_input("Type the lenght of any side: ")
    while type(side) != float:
        try:
            side = float(side)
        except:
            side = raw_input("Please type the length of any side! ")

    res = square(side)
    print "All sides has the lenght:", side
    print "The perimeter is then:", res[0]
    print "The area is:", res[1]
    quit = raw_input("Press return key to close program ")

elif choice == 2:
    print "RECTANGLE - [ ]"
    base = raw_input("Type the length of the base: ")
    height = raw_input("Type the height: ")
    while type(base) != float or type(height) != float:
        try:
            base = float(base)
            height = float(height)
        except:
            base = raw_input("Please type a the length of the base! ")
            height = raw_input("Please type a the height! ")

    res = rect(base, height)
    print "The base is:", base, "and the height is:", height
    print "The perimeter is:", res[0]
    print "The area is:", res[1]
    quit = raw_input("Press return key to close program ")

elif choice == 3:
    print "CIRCLE - O"
    dia = raw_input("Type in the diameter: ")
    while type(dia) != float:
        try:
            dia = float(dia)
        except:
            dia = raw_input("Please type the length of the diameter! ")

    res = circle(dia)
    print "A circle with the diameter:", dia
    print "has a circumference of:", res[0]
    print "and an area of:", res[1]
    quit = raw_input("Press return key to close program ")
    
elif choice == 4:
    print "TRIANGLE - <|"
    print "Remember that the triangle needs to be a right triangel\n\
with one of the angles being 90 degrees"
    base = raw_input("Type the length of the base: ")
    height = raw_input("Type the height: ")
    while type(base) != float or type(height) != float:
        try:
            base = float(base)
            height = float(height)
        except:
            base = raw_input("Please type a the length of the base! ")
            height = raw_input("Please type a the height! ")

    res = triangle(base, height)
    print "A trangle with the base lenght:", base, "and the height", height
    print "has a perimeter of:",res[0]
    print "and an area of:",res[1]
    quit = raw_input("Press return key to close program ")
    
print "end"

    


