<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Python in field calculator in Python Snippets Questions</title>
    <link>https://community.esri.com/t5/python-snippets-questions/python-in-field-calculator/m-p/872706#M643</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have two fields: landuse classification numbers (1-5) and soil rating numbers (1-4). The values of the third field, CN values, depend on what numbers the other other two fields are. For example, if the landuse number is 1 and the soil rating number is 3, the CN value should be 70. I'm attempting to do this via python if-then statement in the field calculator but keep running in problems with it. The most common error message is that it has invalid parameters. Here is what I am trying. I'm completely new to python and would appreciate any help!! &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;IMG class="image-1 jive-image" height="569" src="https://community.esri.com/legacyfs/online/86401_pastedImage_0.png" style="width: 474px; height: 569.27047146402px;" width="474" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 17 Apr 2015 21:38:07 GMT</pubDate>
    <dc:creator>JessicaKnapp</dc:creator>
    <dc:date>2015-04-17T21:38:07Z</dc:date>
    <item>
      <title>Python in field calculator</title>
      <link>https://community.esri.com/t5/python-snippets-questions/python-in-field-calculator/m-p/872706#M643</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have two fields: landuse classification numbers (1-5) and soil rating numbers (1-4). The values of the third field, CN values, depend on what numbers the other other two fields are. For example, if the landuse number is 1 and the soil rating number is 3, the CN value should be 70. I'm attempting to do this via python if-then statement in the field calculator but keep running in problems with it. The most common error message is that it has invalid parameters. Here is what I am trying. I'm completely new to python and would appreciate any help!! &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;IMG class="image-1 jive-image" height="569" src="https://community.esri.com/legacyfs/online/86401_pastedImage_0.png" style="width: 474px; height: 569.27047146402px;" width="474" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 17 Apr 2015 21:38:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/python-in-field-calculator/m-p/872706#M643</guid>
      <dc:creator>JessicaKnapp</dc:creator>
      <dc:date>2015-04-17T21:38:07Z</dc:date>
    </item>
    <item>
      <title>Re: Python in field calculator</title>
      <link>https://community.esri.com/t5/python-snippets-questions/python-in-field-calculator/m-p/872707#M644</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I figured it out and now it works like a charm! I started to continue with additional if statements but the difference was to use == instead of = and to use quotations around the return field.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/86597_pastedImage_0.png" style="max-width: 1200px; max-height: 900px;" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 17 Apr 2015 22:05:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/python-in-field-calculator/m-p/872707#M644</guid>
      <dc:creator>JessicaKnapp</dc:creator>
      <dc:date>2015-04-17T22:05:12Z</dc:date>
    </item>
    <item>
      <title>Re: Python in field calculator</title>
      <link>https://community.esri.com/t5/python-snippets-questions/python-in-field-calculator/m-p/872708#M645</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;some syntactical errors&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;missing a final ) in the def statement&lt;/LI&gt;&lt;LI&gt;equality check is == , not = ... the later is an assignment statement&lt;/LI&gt;&lt;LI&gt;I use &amp;amp; instead of 'and' ... prefer &amp;amp; but six of one... ( lu == 1) and (soil == 3)&lt;/LI&gt;&lt;LI&gt;enclose conditions in ( ) juuuust to be sure&lt;/LI&gt;&lt;LI&gt;always provide a return of some sort, otherwise it is a crap shoot&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ok format&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; def cn(lu,soil):
...&amp;nbsp; if (lu == 1) &amp;amp; (soil== 3):
...&amp;nbsp; return 70
... 
&amp;gt;&amp;gt;&amp;gt; lu = 1
&amp;gt;&amp;gt;&amp;gt; soil = 3
&amp;gt;&amp;gt;&amp;gt; cn(lu,soil)
70
&amp;gt;&amp;gt;&amp;gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;better format&amp;nbsp; (I don't share perfect &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; def cn(lu,soil):
...&amp;nbsp; if (lu == 1) &amp;amp; (soil== 3):
...&amp;nbsp; val = 70
...&amp;nbsp; else:
...&amp;nbsp; val = -999
...&amp;nbsp; return val
...
&amp;gt;&amp;gt;&amp;gt; cn(1,4)
-999
&amp;gt;&amp;gt;&amp;gt; cn(1,3)
70
&amp;gt;&amp;gt;&amp;gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 10:54:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/python-in-field-calculator/m-p/872708#M645</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-12T10:54:57Z</dc:date>
    </item>
    <item>
      <title>Re: Python in field calculator</title>
      <link>https://community.esri.com/t5/python-snippets-questions/python-in-field-calculator/m-p/872709#M646</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;^ missing final ) after "I don't share perfect" &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 17 Apr 2015 22:24:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/python-in-field-calculator/m-p/872709#M646</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2015-04-17T22:24:27Z</dc:date>
    </item>
    <item>
      <title>Re: Python in field calculator</title>
      <link>https://community.esri.com/t5/python-snippets-questions/python-in-field-calculator/m-p/872710#M647</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It is those damn smiley faces...they corrupt the end of the line (&lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/wink.png" /&gt;)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 17 Apr 2015 22:29:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/python-in-field-calculator/m-p/872710#M647</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2015-04-17T22:29:18Z</dc:date>
    </item>
    <item>
      <title>Re: Python in field calculator</title>
      <link>https://community.esri.com/t5/python-snippets-questions/python-in-field-calculator/m-p/872711#M648</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;UL&gt;&lt;LI&gt;always provide a return of some sort, otherwise it is a crap shoot&lt;/LI&gt;&lt;/UL&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;I like this suggestion from &lt;A href="https://community.esri.com/migrated-users/3116"&gt;Dan Patterson&lt;/A&gt;​. In addition &lt;A href="https://community.esri.com/migrated-users/3420"&gt;Joshua Bixby&lt;/A&gt;​ recommends using one return for &lt;A _jive_internal="true" href="https://community.esri.com/message/429379#429379"&gt;a single exit point&lt;/A&gt; (like Dan's "better format" code above), rather than having multiple returns. The &lt;A href="http://docs.python-guide.org/en/latest/writing/style/#returning-values"&gt;Python Code Style&lt;/A&gt; agrees.&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;When a function grows in complexity it is not uncommon to use multiple return statements inside the function’s body. However, in order to keep a clear intent and a sustainable readability level, it is preferable to avoid returning meaningful values from many output points in the body.&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Apr 2015 15:31:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/python-in-field-calculator/m-p/872711#M648</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2015-04-24T15:31:49Z</dc:date>
    </item>
  </channel>
</rss>

