# Performing multiple if statements with ArcPy UpdateCursor?

Beginner with Python trying to perform multiple calculations on a queried set of rows. I'm am then looping thru a set of queries. But I need to perform all calcuations on returned rows before starting next query in the array. The first set of if else statements runs correctly. The subsequent statements don't update every record. Here's what I have so far...

``````for fl in flArray:
calcFields = ['FL', fl, 'EValue', 'Diff', 'OOC', 'E_GRTR', 'gCAT', 'eCAT']
queryFL = flArray[fl]
cursor = arcpy.da.UpdateCursor(out_layer_file, calcFields, queryFL)

# Calculate difference
for row in cursor:
row = row - row
if row > 0:
row = 1
else:
row = 0
cursor.updateRow(row)

### Calculate g Category

for row in cursor:
if row < .15:
row = 0
elif .15 <= row <= .22:
row = 1
elif .23 <= row <= .3:
row = 2
elif .31 <= row <= .37:
row = 3
elif .38 <= row <= .45:
row = 4
elif row > .45:
row = 5
cursor.updateRow(row)

### Calculate e Category

for row in cursor:
if row < .15:
row = 0
elif .15 <= row <= .22:
row = 1
elif .23 <= row <= .3:
row = 2
elif .31 <= row <= .37:
row = 3
elif .38 <= row <= .45:
row = 4
elif row > .45:
row = 5
cursor.updateRow(row)

### Calculate OOC

for row in cursor:
row = abs(row - row)
cursor.updateRow(row)
cursor.reset
``````
• Multiple elif can be squeezed gis.stackexchange.com/questions/177652/… – FelixIP Jan 15 '17 at 22:04
• Values in the range (0.22,0.23), and in all the other gaps, will not have any values set. This is poor coding practice. – Vince Jan 15 '17 at 22:13
• @Vince I fixed ranges to include the gaps. Thanks for catching this – Jotter Jan 18 '17 at 17:39

This is not highly Pythonic but as a quick fix try the following. Rather than trying to backup using the `reset` method, simply run through the table four times. By using `with` syntax there is no need to clean up the `row` and `cursor` objects when each cursor completes.

``````for fl in flArray:
calcFields = ['FL', fl, 'EValue', 'Diff', 'OOC', 'E_GRTR', 'gCAT', 'eCAT']
queryFL = flArray[fl]

# Calculate difference

with arcpy.da.UpdateCursor(out_layer_file, calcFields, queryFL) as cursor:
for row in cursor:
row = row - row
if row > 0:
row = 1
else:
row = 0
cursor.updateRow(row)

### Calculate g Category
with arcpy.da.UpdateCursor(out_layer_file, calcFields, queryFL) as cursor:
for row in cursor:
if row < .15:
row = 0
elif .15 <= row <= .22:
row = 1
elif .23 <= row <= .3:
row = 2
elif .31 <= row <= .37:
row = 3
elif .38 <= row <= .45:
row = 4
elif row > .45:
row = 5
cursor.updateRow(row)

### Calculate e Category
with arcpy.da.UpdateCursor(out_layer_file, calcFields, queryFL) as cursor:
for row in cursor:
if row < .15:
row = 0
elif .15 <= row <= .22:
row = 1
elif .23 <= row <= .3:
row = 2
elif .31 <= row <= .37:
row = 3
elif .38 <= row <= .45:
row = 4
elif row > .45:
row = 5
cursor.updateRow(row)

### Calculate OOC
with arcpy.da.UpdateCursor(out_layer_file, calcFields, queryFL) as cursor:
for row in cursor:
row = abs(row - row)
cursor.updateRow(row)
``````