# Using Python to Calculate a field

I am trying to replace values in a single column using an else if statement, but I keep getting an Syntax error in line 2. I'm new to Python so it's probably an easy solution, but I haven't been able to figure it out. I am using ArcMap 10.1.

``````    def Replace(SrcLevel):
if SrcLevel == 0:
return "4-Other"
elif SrcLevel == 1:
retun "1-Primary"
elif SrcLevel == 2:
return "2-Middle"

SrcLevel = Replace(!SrcLevel!)
``````
-
==instead of = ???? – Dan Patterson Feb 1 '13 at 22:37
Take your function and paste it into a new empty Python script, then call it a few times with some dummy values to test the logic. Step through it with a debugger if you aren't sure what is going on. I recommend PyScripter as it's one of the better IDEs out there for Windows. Editing and testing Python code from within the Field Calculator is one of the most frustrating experiences imaginable for a beginner, so don't do it. – blah238 Feb 1 '13 at 23:22
yes, I'll second that, I couldn't even get one of the ESRI VBA sample scripts to work in it yesterday.... – Banger Feb 2 '13 at 0:17
question (I'm just learning python too) what happens if the SrcLevel isn't a 0, 1, or 2? – Banger Feb 2 '13 at 0:29
@Banger, Field Calculator will enter null if you don't return something. For example a function that only has the line `pass` in it will set all values to null. If you don't want this I would probably return the orginal field value in a final `else` statement. – blah238 Feb 2 '13 at 1:25

At least four things needed correction:

1. You need to use return rather than print
2. You need to use == rather than = on your if and elif statements (thanks @DanPatterson)
3. Take care with your indentation - your print (to become return) statements don't seem to line up
4. return statement was written in one place as retun [sic]

There are examples here.

-
Thanks for the help. I am no longer getting a syntax error for line 2, but now I'm getting a general error. Any other suggestions? The link the examples page didn't seem to work for me. – Andrea Feb 1 '13 at 23:17