Help Calculate field based on values in another field

4361
3
08-31-2011 03:11 PM
RichardThurau
Occasional Contributor
Sorry to drop this on the community, but i can't seem to find a suitable example

My target field is ???PRG_Parcel???.
I???m trying to write a function called ???answer??? that will return text to ???PRG_Parcel??? based on values in the ???Taxable??? field.

Input Table: table
Field Name: Field_Name
Expression: answer()
Exp. type: Python
Code Block:
def answer(!Taxable!)
  if Taxable > 20000:
    return "'Y'"
  else:
    return "'N'"

I've tried to do things a few different ways. Would anyone be so kind as to explain how I should be writing this code?

Thanks a bundle.

rich
Tags (2)
0 Kudos
3 Replies
DanPatterson_Retired
MVP Esteemed Contributor
Did you check out the pictoral image in this link down in the "Code Blocks" section?
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//005s0000002m000000.htm
It will visually give you an idea what needs to go where.  Also, there is no indentation in your example ensure that you are using proper indentation ( 2 or 4 spaces) in the def
0 Kudos
StacyRendall1
Occasional Contributor III
You are pretty close. What you had wrong:

  1. The expression needs to pass the field to the code block, so the field (or fields) must go in the brackets there.

  2. The term inside the code block doesn't need to have the same name as the field, or have exclamation marks around it...


The following ought to work:
Expression: 
answer(!Taxable!)

Code Block:
def answer(Tx):
    if Tx > 20000:
        return "'Y'"
    else:
        return "'N'"
0 Kudos
RichardThurau
Occasional Contributor
Stacy,
Exactly the help I was looking for. This helps me with a lot of different things I've been working on.

Thank you!!

Dan,
The visual examples are okay, but as someone who already knows how to program, you may not notice that none of these examples explicitly detail what the variables are describing.
The code was indented properly, it was just dropped in the forums text box.
Thanks for your input though.

Thanks ESRI forums!

Rich
0 Kudos