Original User: Caleb1987 Hi Everyone,
I think I am not understanding the logic, but what I want to do is make an if then statement. I want to create a file and then create another file. The code creates the first file, but does not create the second file. What am I doing wrong?
This is an easy fix. The problem is you used "elif" when you need to use "if". The elif statement means "else if", meaning execute this if the previous "if" (or "elif") statement is not true. If you only wanted to create ONE of these two files, the use of the elif statement would work. But if you just want to this to work if those variables exist, you want to use just if.def main():
try:
import arcpy, sys, traceback, os, string, locale,arcgisscripting
arcpy.env.overwriteOutput = True
from arcpy import env
env.workspace =r"C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar"
outputVGIboundingbox= "VGIboundingbox.shp"
outputGNISboundingbox="GNISboundingbox.shp"
VGIFile = r"C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar\polygon.shp"
GNIS_FC = r"C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar\GNIS.shp"
if arcpy.Exists(outputVGIboundingbox):
arcpy.Delete_management(outputVGIboundingbox)
VGIboundingbox= arcpy.FeatureEnvelopeToPolygon_management(VGIFile,outputVGIboundingbox,"MULTIPART")
if arcpy.Exists(outputGNISboundingbox):
arcpy.Delete_management(outputGNISboundingbox)
GNISboundingbox= arcpy.FeatureEnvelopeToPolygon_management(GNIS_Geom,outputGNISboundingbox,"MULTIPART")
Here is a simpler example to illustrate the proper use of if and elif.
>>> # True/False statement
>>> def GreaterThan(x,y):
if x > y:
print '%s is greater than %s' %(x,y)
elif x == y:
print '%s is equal to %s' %(x,y)
else:
print '%s is less than %s' %(x,y)
>>> GreaterThan(5,7)
5 is less than 7
>>> GreaterThan(3,3)
3 is equal to 3
>>> GreaterThan(8,2)
8 is greater than 2
>>>
In the above example, I used all three: if, elif, and else. The else statement could have easily been another "elif" statement but then I would have had to put "elif x < y:". I used an else statement instead since there were no possibilities other than less than since greater than and equal to were already used.