Python Learning Diary - Area Calculator
The author is not responsible for any flashbacks to math class horrors.
The quest for Python continues. This time the road leads all the way back to geometry class. (Ironic as I spent a day this weekend in a high school cafeteria training for a volunteer project.) I have done a lot of hacking around on PowerShell scripts for work, so my appetite for hobby coding hasn’t been as zealous the last few weeks. I did put this area calculator together. While this was another lesson cribbed from Code Academy, I tried to do a much more exhaustive exploration of available formulas. Here’s the code below.
import math
shape = raw_input("What shape are you measuring? ")
if shape.lower() == "circle":
try:
radius = float(raw_input("What is the radius? "))
area = math.pi * (radius**2)
print area
except:
print "Please use a number next time."
elif shape.lower() == "triangle":
tryType = raw_input("Is it an equalateral? ")
if tryType.lower() == "yes" or tryType.lower() == "y":
try:
side = float(raw_input("What is the length of each side? "))
area = ((math.sqrt(3)/4)*(side**2))
print area
except:
print "Please use a number next time."
else:
try:
base = float(raw_input("What is the length of the triangle's base? "))
height = float((raw_input("What is the length of the triangle's height? ")))
area = ((.5)*(base * height))
print area
except:
print "Please use a number next time."
elif shape.lower() == "square":
try:
side = float(raw_input("What is the length of each side? "))
area = (side ** 2)
print area
except:
print "Use numbers next time."
elif shape.lower() == "oval" or shape.lower() == "ellipse":
try:
r1 = float(raw_input("what is the length of the shorter radius? "))
r2 = float(raw_input("What is the length of the longer radius? "))
area = (math.pi*(r1 * r2))
print area
except:
print "Use a number next time."
elif shape.lower() == "rectangle":
try:
short = float(raw_input("What is the length of the short side? "))
long = float(raw_input("What is the length of the long side? "))
area = short * long
print area
except:
print "Use a number next time."
elif shape.lower() == "parallelogram":
try:
base = float(raw_input("What is the length of the base? "))
height = float(raw_input("What is the length of the height? "))
area = base * height
print area
except:
print "Use a number next time."
elif shape.lower() == "trapezoid":
try:
top = float(raw_input("What is the length of the top? "))
base = float(raw_input("What is the length of the base? "))
height = float(raw_input("what is the height of the trapezoid? "))
area = ((height/2)*(base+top))
print area
except:
print "Use a number next time."
else:
print "Sorry, not sure how to do that one."
To be honest, this was a simple script to put together. After doing a bit of research to find all of the math, the rest was just remembering syntax. To get the need constants and methods, I did need to import the Math module. Once that is added, square root and pi are ready to use. Python gives them easy names, sqrt and pi.
Some of the formulas also needed exponents, which are included by default. You call these by using **. Other than that, the hardest part was thinking up how to ask for all for the different shapes to get the right info.
This is a program I would like to come back to with a little more experience. I would prefer to have a UI here, illustrating the shape and pointing to the measurement I ask for. Outside of learning this for work, I have been interested in possibly looking into using Python for more analytical applications. Finding out that the math is easy to pick up and powerful means it shouldn’t be hard to figure out more advanced math applications.
Rants and Reviews. Mostly just BS and Affiliate Links.
Follow on Mastodon