If statement in Python

725
9
05-09-2017 10:36 AM
Highlighted
by
New Contributor III

Hello,

I have two attributes for counties and districts. I need to write a script in Python to get the District IDs based on the counties. I am not a developer so I may have mistakes in my script attached. I appreciate your help.

Thanks.

Reply
0 Kudos
9 Replies
Highlighted
MVP Esteemed Contributor

Notice the syntax in the following example

you have to have a function (ie def reclass) which you pass parameters to (ie a), but in the expression box, you will notice that you pass a field name enclosed in double quotes to the function (ie reclass(!Id!) in the example.

Now the function is specific about indentation.  4 spaces is standard, with the def line unindented.  The if statement in my example does an equality check notice the colon at the end.  If the equality is met, then the variable 'a' is assigned 1,  it is indented 4 spaced.  The 'else' section is where program focus moves it a doesn't equal 1, whereby a is assigned 0.  Finally a value is returned.  Now you can proceed to fix your code.  In future, it would be better to copy and paste the snippet, so that people can edit your exact example without having to refer back to an image.

Good luck

Highlighted
by
New Contributor III

Thanks or your response. I followed all your instructions (please see the image attached). But it does not work. By the way, there are 33 counties, but I am just trying the code for two counties for now. Also, the County field is a string attribute and District is short integer.

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

post your code since your indentation is not correct in the if block since 4 spaces per indentation level.  If you have many to reclass, then an if else approach is not the one to take.

Reply
0 Kudos
Highlighted
by
New Contributor III

def reclass(a):
    if a == 'Wicomico':
    return 1
 elif a == 'Cecil':
     return 2
    else:
     return 0

Reply
0 Kudos
Highlighted
by
New Contributor III

I need to classify 33 counties (for 287 camera records) into 7 districts. If I don't use if statement, what else can I use? Thanks

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

a nested if statement with 33 comparisons is not the route to go.  producing a separate table for join/relates to do the conversion is what I was referring to.

Highlighted
by
New Contributor III

Okay, Thanks again!

Reply
0 Kudos
Highlighted
Regular Contributor II

If your If statement do return you do not need to use elif

for example

if a == 'Wicmico': return 1

if a == 'Cecil': return 2

Then it does not look so wrong.

Highlighted
MVP Esteemed Contributor

When you say it isn't working, what do you mean, exactly?  Is it generating an error?  If so, post the error.  Is it giving unexpected results?  What are those results and how do they differ from what you expect?

Also, what you just posted here doesn't line up exactly with your screenshot.  The screenshot appears to show an extra gap between the 2 equals signs for the second comparison.  Also, your indentation above is all off, this is how it should look when/if using 4 spaces:

def reclass(a):
    if a == 'Wicomico':
        return 1
    elif a == 'Cecil':
        return 2
    else:
        return 0