tynation.com

Thursday, June 2, 2011

Just a code for work.

So today (and yesterday) I found myself converting numbers for a 1" to 1' scale. Finding out how many feet there was was easy. For every 1" there was 1'. Then I was left with measurements like 5.2356" So I just wrote down that. I got tired of using the calculator to do these conversions so I wrote a little program for it.



num = 1
inches = 0
while (num != 0):
     print "enter number after decimal point or 0 to quit"
     num = input()
     if (num > 9999):
          print "use fewer numbers"
     elif (num > 999):
          num = num * .0001
     elif (num > 99):
          num = num * .001
     elif (num > 9):
          num = num * .01
     elif (num > 0):
          num = num * .1
     inches = num * 12
     print inches, "inches"

This worked decently. I didnt have to enter a decimal for anything and it converted the number with as few keystrokes as possible. But there was a problem. When I had .0901 inches the program crashed. Luckily I could actually enter ".0901" and it would convert that just fine. I call that an unintended feature


I dont know how good my builders are good at math but 3.276" is very difficult to translate into the widely used half, quarter, eighth, etc units for inch fractions. So I decided that when I had some time I would work a little bit harder and create something that made my life even easier still.



def startup():
     print " _____________________Inch To Foot Scale____________________"
     print "|This program will convert measurements in a 1\" to 1' scale|"
     print "| Just input value in inches (complete with decimal points) |"
     print "|_______________________Input 0 to quit_____________________|"
     print " "
     print "enter value to be converted"
     print " "


def fracFinder(frac):
     sixFourth = .015625
     fraction = "error"
     if (frac < 3 * sixFourth):
          fraction = '1/32"'
     elif (frac < 5 * sixFourth):
          fraction = '1/16"'
     elif (frac < 7 * sixFourth):
          fraction = '3/32"'
     elif (frac < 9 * sixFourth):
          fraction = '1/8"'
     elif (frac < 11 * sixFourth):
          fraction = '5/32"'
     elif (frac < 13 * sixFourth):
          fraction = '3/16"'
     elif (frac < 15 * sixFourth):
          fraction = '7/32"'
     elif (frac < 17 * sixFourth):
          fraction = '1/4"'
     elif (frac < 19 * sixFourth):
          fraction = '9/32"'
     elif (frac < 21 * sixFourth):
          fraction = '5/16"'
     elif (frac < 23 * sixFourth):
          fraction = '11/32"'
     elif (frac < 25 * sixFourth):
          fraction = '3/8"'
     elif (frac < 27 * sixFourth):
          fraction = '13/32"'
     elif (frac < 29 * sixFourth):
          fraction = '7/16"'
     elif (frac < 31 * sixFourth):
          fraction = '15/32"'
     elif (frac < 33 * sixFourth):
          fraction = '1/2"'
     elif (frac < 35 * sixFourth):
          fraction = '17/32"'
     elif (frac < 37 * sixFourth):
          fraction = '9/16"'
     elif (frac < 39 * sixFourth):
          fraction = '19/32"'
     elif (frac < 41 * sixFourth):
          fraction = '5/8"'
     elif (frac < 43 * sixFourth):
          fraction = '21/32"'
     elif (frac < 45 * sixFourth):
          fraction = '11/16"'
     elif (frac < 47 * sixFourth):
          fraction = '23/32"'
     elif (frac < 49 * sixFourth):
          fraction = '3/4"'
     elif (frac < 51 * sixFourth):
          fraction = '25/32"'
     elif (frac < 53 * sixFourth):
          fraction = '13/16"'
     elif (frac < 55 * sixFourth):
          fraction = '27/32"'
     elif (frac < 57 * sixFourth):
          fraction = '7/8"'
     elif (frac < 59 * sixFourth):
          fraction = '29/32"'
     elif (frac < 61 * sixFourth):
          fraction = '15/16"'
     else:
          fraction = '31/32"'
     return fraction
entry = 1
feet = 0
inches = 0
frac = 0
entryInt = 0
entryDecimal = 0
inchesInt = 0
inchesDecimal = 0
startup()
while (entry != 0):
     entry = input()
     entryInt = int(entry)
     entryDecimal = entry - entryInt
     inches = entryDecimal * 12
     inchesInt = int(inches)
     inchesDecimal = inches - inchesInt
     print entryInt, "'", inchesInt, fracFinder(inchesDecimal)
     print " "
     print "enter value to be converted"
     print " "

A little bit of design, a little bit of function, and a lot of usefulness. I actually had to look up the int() function, but boy was it useful in creating this program. now this will actually take an input of inches on my scale drawing, and kick out actual feet measurements down to the 32nd of an inch. ex: 2 ' 3 1/2"


Not even truly a programmer, yet I've already found myself writing a program of great use for me at work.


Now if I only knew how to create a tool in Illustrator that made a line segment, did this calculation, and displayed it near said line segment. Then I'd be making some cash! Perhaps I should continue learning programming...


-Ty

1 comment:

T said...

Here's my version.

http://pastebin.com/McZHhzK0