Select to view content in your preferred language

Need Help with Python Syntax

1426
9
Jump to solution
02-24-2012 03:47 AM
AlanToms
Regular Contributor
Hello folks,

I need help with the proper syntax. I'm sure it's easy I just cant get it.  Basically it's telling me that the syntax for my return value, Str(!SHIELD_VAL!), is invalid.  I'm using a field calculator to run a check on field HWY_Type and given certain values, it will populate field FEDROUTE with the value of SHIELD_VAL.  Any help would be appreciated.

Thank you
Alan

def FedRoute(tvalue):     value = tvalue     if value == 'INT':         return Str(!SHIELD_VAL!)     elif value == 'US':         return Str(!SHIELD_VAL!)  FedRoute(!HWY_Type!)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LeonardWilliamson
Occasional Contributor
Oops, I was testing the code and forgot to change the values to the original

View solution in original post

0 Kudos
9 Replies
LeonardWilliamson
Occasional Contributor
Alan,

You need to include the SHIELD_VAL field as an input variable in order to read the values.  The code below should fix your problem.

def FedRoute(tvalue, shield):
    value = tvalue
    shield = ''
    if value == 5:
        shield = str(shield)
    elif value == 9:
        shield =  str(shield)
    return shield

FedRoute(!HWY_Type!, !SHIELD_VAL!)
0 Kudos
DarrenWiens2
MVP Alum
Won't the above code result in an empty string, every time?

Instead:
def FedRoute(tvalue, shield):
    if tvalue == 'INT':
        return str(shield)
    elif tvalue == 'US':
        return str(shield)

FedRoute(!HWY_Type!, !SHIELD_VAL!)
0 Kudos
LeonardWilliamson
Occasional Contributor
Oops, I was testing the code and forgot to change the values to the original
0 Kudos
AlanToms
Regular Contributor
I tried both examples (Leonard's modified to my data, Darrens' exaclty)  and they both returned the following error.  I looked up 'exceptions.SyntaxError' but I do not understand this enough to solve the issue.  Any other suggestions?  Thanks for the help on this. It is appreciated.

ERROR 000539: Error running expression: FedRoute("INT", "75
") <type 'exceptions.SyntaxError'>: EOL while scanning string literal (<string>, line 1)
Failed to execute (Calculate Field (FEDROUTE)).
0 Kudos
DarrenWiens2
MVP Alum
Did you choose Python as the parser?
0 Kudos
AlanToms
Regular Contributor
Yes, I choose Python 9.3
0 Kudos
LeonardWilliamson
Occasional Contributor
Are "Int" and "US" the only values?
0 Kudos
AlanToms
Regular Contributor
Yeah I seem to have NULL values in both the HWY_Type and SHIELD_VAL fields that I think is causing the problem.  I'll take a look at it again on Monday.
0 Kudos
AlanToms
Regular Contributor
The code samples you both provided worked.  I discovered some issues with the data iteself that didnt want to work. 

Thank you for the help.
Alan
0 Kudos