Error 000539: Error running expression. UnboundlocalError: local varialble 'Value' referenced before assignment

138
7
Jump to solution
01-25-2019 11:14 AM
ChristaRiley
New Contributor

I am using the Calculate Field tool in model builder. I am new to Python, but modeled my code after something I had done in a previous model that worked well. Despite following similar syntax and rules, I am given an error message I don't recognize (see attachments). I’ve tried looking up similar issues and have not been able to find a solution. Current Depth contains numerical data.
Any insight would be appreciated.

Here is my code:

def assignValue(DWType, CurrentDepth):

   if DWType == 'single':  

       Value = ((90 - CurrentDepth) / 90) * 100

   if DWType == 'double':  

       Value = ((140 - CurrentDepth) / 140) * 100

   if DWType == 'triple':  

       Value = ((190 - CurrentDepth) / 190) * 100

   return Value

 

Codeblock:

assginValue( !DRYW_DEPTH!, !Cur_BotDep!)

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

maybe you are missing a value, or one has an error in spelling, or a blank but assign a default value to check

def assignValue(DWType, CurrentDepth):
Value = -999 # add this ad the top
....

View solution in original post

0 Kudos
7 Replies
DanPatterson_Retired
MVP Esteemed Contributor

maybe you are missing a value, or one has an error in spelling, or a blank but assign a default value to check

def assignValue(DWType, CurrentDepth):
Value = -999 # add this ad the top
....

View solution in original post

0 Kudos
ChristaRiley
New Contributor

This applied all the values I had selected as -999, but no error message. So it seems like Joe said, none of my statements are being evaluated as true. So I suppose I will have to dig deeper there to figure out why. Thanks for the help!

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

DWType  check the text case for that field since Single, single and SINGLE are all different.

You can alter your condition to check for leading trailing spaces as well as the case issue

lower(str(DWType).strip())

0 Kudos
JoeBorgione
MVP Esteemed Contributor

I think what's going is none of your if statements are being evaluated to true, therefore when you try to return Value, it it has not been cast.  Note your formatted script below and line 8 (the offending line from your error) supports my idea.

Check your math on one selected record and make sure it's good. In your model, are there any selections being made before you apply this calculation?  Also are your sure the values of DWType are exactly as you show them?  Remember, python is case sensitive.   Finally as a matter of coding style I would not use individual if statements but rather if, elif as shown below your original code.

def assignValue(DWType, CurrentDepth):
if DWType == 'single':
Value = ((90 - CurrentDepth) / 90) * 100
if DWType == 'double':
Value = ((140 - CurrentDepth) / 140) * 100
if DWType == 'triple':
Value = ((190 - CurrentDepth) / 190) * 100
return Value‍‍‍‍‍‍‍‍

def assignValue(DWType, CurrentDepth):
if DWType == 'single':
Value = ((90 - CurrentDepth) / 90) * 100
elif DWType == 'double':
Value = ((140 - CurrentDepth) / 140) * 100
elif DWType == 'triple':
Value = ((190 - CurrentDepth) / 190) * 100
else:
Value = -999
return Value‍‍‍‍‍‍‍‍
can't wait to retire....
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Joe, if you put that Value = -999 at the top as I suggested, it has the same result as your lines 16 and 17

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Yep....  (I was typing my response as you posted yours; ironically we both chose -999 as the default value....)  

can't wait to retire....
0 Kudos
JoeBorgione
MVP Esteemed Contributor

Not sure if this is it....

def assignValue(DWType, CurrentDepth):

versus

assginValue( !DRYW_DEPTH!, !Cur_BotDep!)

(Should have errored out...)

can't wait to retire....
0 Kudos