Python in Field Calculator - mod function

2636
3
Jump to solution
01-16-2014 09:42 AM
Zeke
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)
0 Kudos
1 Solution

Accepted Solutions
JoshuaChisholm
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?

View solution in original post

3 Replies
JoshuaChisholm
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?
SurajBharati
New Contributor

The best website for Download Mod Apk for free

0 Kudos
Zeke
by
Regular Contributor III
Thanks, that worked great. Yes, LineTyp10 is an int. Much appreciated.
0 Kudos