3

I'm trying to do some conditional formatting of an attribute table using Field Calculator. I tried using code below, but am not getting any returns that meet conditions specified (even though the conditions exist). What am I doing wrong? I am new to coding, so please assume I am ignorant to obvious mistakes.

My steps so far:

  1. Created a new field "Raster_code" (this is the field where I am using field calculator)
  2. Open field calculator, set parser to Python, enter the following pre-logic script:
def ifBlock(BGCLMTCLBL,STSRSLBLC1):
    if 'BGCLMTCLBL' == 'AT' and 'STSRSLBLC1' == '00':
        return 1
    else:
        return 0

Note: "BGCLMTCLBL" and "STSRSLBLC1" are two separate fields. I want to locate records containing a certain combination of values from these respective fields

  1. Enter the following in the Raster_code block:
ifBlock(!BGCLMTCLBL!,!STSRSLBLC1!)

The result I get is 0 for all records-- in other words, the conditions were not met.

Here is a screenshot of the above-noted fields in my attribute table. screenshot of attribute table

1

You need to remove the quotes around the field names on second row:

def ifBlock(BGCLMTCLBL,STSRSLBLC1):
    if BGCLMTCLBL == 'AT' and STSRSLBLC1 == '00':
        return 1
    else:
        return 0

If not you are asking python if the string 'BGCLMTCLBL' is the same as 'AT' which it isnt. BGCLMTCLBL without quotes is the variable holding your field values.

| improve this answer | |
  • Note that ALLCAPS variable names are not best practice in Python. – Vince Apr 24 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.