Null if Statement in Calculated Field

1371
5
02-13-2020 03:47 AM
ThomasKelly
New Contributor II

I have a column in an attribute table that has a mixture of nulls and data. I wish to create a new column that will return a "yes" if there is data in the column being checked and "no" if it is a null. I have tried a variety of python functions but I keep getting an error similar to the below:

 

ERROR 000539: File "<expression>", line 1
updateValue(1E+21.0)
^
SyntaxError: invalid syntax
Failed to execute (CalculateField).   

I have tried the following :

X = updateValue(!field!)

def updateValue(value):
   if value == None:
   return 'no'
   else: return "yes"

Any help would be most appreciated. 

0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus

/blogs/dan_patterson/2016/08/14/script-formatting 

your indentation was wrong and the code block (eg lines 3-6 or 17-20 ) need to be placed in the code section and the expression (line 10 or 24) in the expression line.

Make the field (!some_field!) active, python parser, put in the code in the code block and the expression in its place and run.

Also don't use == None, use ... is None ...

you don't need your extra else since it will bypass to line 6 if value is not None

# for string field

def updateValue(value):
   if value is None:
       return 'no'
   return "yes"

# calling expression


updateValue(!some_field!)


# ------------------------
# for numeric field


def updateValue(value):
   if value is None:
       return -999
   return value

# calling expression

updateValue(!some_field!)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
ThomasKelly
New Contributor II

Hi Dan, 

Thank you for the response, I have tried both of these and whilst both expressions are valid I am still returning the following for both:

ERROR 000539: File "<expression>", line 1
updateValue(1E+21.0)
^
SyntaxError: invalid syntax
Failed to execute (CalculateField).

The (!some_field!) is of data type double ...

I cannot understand why the same error is returning, it should be a straightforward process.

Many thanks 

Thomas  

0 Kudos
DanPatterson_Retired
MVP Emeritus
1e2
100.0

1e2.0
  File "<ipython-input-2-3dea3e3816b2>", line 1
    1e2.0
        ^
SyntaxError: invalid syntax

spot the difference?

ThomasKelly
New Contributor II

is it a decimal formatting issue ? 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

This question seems to come up in one form or another about every 6 months on GeoNet.  I think it is important to understand what Dan is pointing out.  Additionally, you can use a Python conditional expression to achieve the result while not needing to define a code block:

"NO" if !field! is None else "YES"