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!)
Solved! Go to Solution.
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
....
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
....
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!
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())
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
Joe, if you put that Value = -999 at the top as I suggested, it has the same result as your lines 16 and 17
Yep.... (I was typing my response as you posted yours; ironically we both chose -999 as the default value....)
Not sure if this is it....
def assignValue(DWType, CurrentDepth):
versus
assginValue( !DRYW_DEPTH!, !Cur_BotDep!)
(Should have errored out...)