Edwakurt

Convert python script to ArcMap Script tool

Discussion created by Edwakurt on Mar 16, 2011
Latest reply on Mar 21, 2011 by geosi
I am new to using python scripting and need help making my python script into a script tool.  My script converts area measurments into other area units.  It runs fine in pythonwin and gives me correct output but when I try to attach it to a script tool it just bombs.  I could use all the help that anybody would be willing to give me.  Below is my coding for the conversions.  Thanks.

#create an empty dictionary
areaD = {}
# populate dictionary using indexing and assignment with units and conversion factors relative to sqmeter = 1.0
# to convert x sqmeters to any of the other area units multiply by the factor
# to convert x of any of the other area units to sqmeter divide by the factor
# to convert x of any area unit to any of the other area units go over interim sqmeter
# this minimizes the total number of conversion factors
areaD['sqmeter']      = 1.0
areaD['sqmillimeter'] = 1000000.0
areaD['sqcentimeter'] = 10000.0
areaD['sqkilometer']  = 0.000001
areaD['hectare']      = 0.0001
areaD['sqinch']       = 1550.003
areaD['sqfoot']       = 10.76391
areaD['sqyard']       = 1.19599
areaD['acre']         = 0.0002471054
areaD['sqmile']       = 0.0000003861022


def convertArea(x, unit1, unit2):
    """area conversion with error trapping"""
    if (unit1 in areaD) and (unit2 in areaD):
        factor1 = areaD[unit1]
        factor2 = areaD[unit2]
        return factor2*x/factor1
    else:
        return False
number=1
while x != 0:
    number = raw_input ("Number of Units:")
    unit1 = raw_input ("Unit Converting From:")
    unit2 = raw_input ("Unit Converting To:")
    outcome = convertArea(x, unit1, unit2)
    if outcome is not False:
        print "%f %s = %f %s" % (x, unit1, outcome, unit2)
    else:
        print "There was an error converting %s to %s"  % (unit1,unit2)

Outcomes