Select to view content in your preferred language

# Python in Field Calculator - mod function

3822
3
01-16-2014 09:42 AM
by
Regular Contributor III
There are two fields in a topo line feature class, Elev for elevation and LineType10. I want to set LineType10 to 1 if Elev % 10 == 0, and 2 otherwise. The purpose is to be able to symbolize at ten foot intervals. I've got the code below, which doesn't give errors but also doesn't set any values. I've also used the plain % mod symbol. Any ideas why this doesn't run? Thanks.

Pre-Logic
`def SetTen(elv, line):   if math.fmod(elv) == 0:     line = 1   else:     line = 2   return line`

Window
`SetTen( !Elev!, !LineType10!)`
Tags (2)
1 Solution

Accepted Solutions
Regular Contributor
Hello Greg,

I'm not sure exactly why your code isn't working, but it might be good to simplify your code a little. First, you are calculating the value of LineType10, so you don't need to pass it as a variable to the function. You can also just return the value for line instead of setting and then returning a variable called line.

Does this work for you?
Pre-Logic:
`def isTen(elev):   if elev%10==0:     return 1   else:     return 2`

Expression:
`isTen( !Elev! )`

Also, just to confirm, LineType10 is a int field right?
3 Replies
Regular Contributor
Hello Greg,

I'm not sure exactly why your code isn't working, but it might be good to simplify your code a little. First, you are calculating the value of LineType10, so you don't need to pass it as a variable to the function. You can also just return the value for line instead of setting and then returning a variable called line.

Does this work for you?
Pre-Logic:
`def isTen(elev):   if elev%10==0:     return 1   else:     return 2`

Expression:
`isTen( !Elev! )`

Also, just to confirm, LineType10 is a int field right?
New Contributor