ArcGIS Field Calculator – Writing If/Then Statements with Python Parser

arcgis-10.1arcgis-desktopfield-calculatorif elsepython-parser

I have gotten stuck on seemingly simple ArcGIS field calculator python expression.

I obtained a great answer to a another similar question a few years ago: Using Range in Python expression of ArcGIS Field Calculator?.
Now I need to populate a new field based on 6 ranges of values (bedrock depth).

My attempt at writing this expression is below:

enter image description here

I just need to populate the empty field "bedrockdp" using the ranges in the toc called brockdepmin.

screen shot

Pre-Logic Script Code:

def !GRIDCODE! (value):
    if value >0 and value < 50:
        return "0 - 50 cm from the surface"
    else:
        return "n/a"

bedrockdep =

GRIDCODE (!bedrockdep!)

Best Answer

I think the problem lies with your statement:

def !gridcode! (value):

is incorrect, see http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004s000000

def something(value):

It's hard to read what you've got there but I think this is it: Python expression

Full expression:

def TextValue(FromCode):
    if FromCode > 0 and FromCode < 50:
        return "0 - 50 cm from the surface"
    elif FromCode < 150:
        return "101 - 150 cm"
    elif FromCode < 200:
        return "151 - 200 cm"
    elif FromCode < 250:
        return "201 - 250 cm"
    elif FromCode < 1000:
        return "251 - 1000 cm"
    else:
        return "N/A"

This will populate the text that is in the legend using the GRIDCODE as the numeric source.

Related Question