<?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 Re: trying to write a subroutine in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/trying-to-write-a-subroutine/m-p/560510#M43843</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks!!&amp;nbsp; That worked!!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Amy&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 26 Oct 2011 18:09:38 GMT</pubDate>
    <dc:creator>AmyMeehan</dc:creator>
    <dc:date>2011-10-26T18:09:38Z</dc:date>
    <item>
      <title>trying to write a subroutine</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-write-a-subroutine/m-p/560508#M43841</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm trying to write a subroutine for the first time and I'm getting very confused.&amp;nbsp; I'm trying to create a tool where users would interactively add a point to a feature class (using a feature set and appending it to the original feature class).&amp;nbsp; The tool uses the interactive feature input control.&amp;nbsp; So far I've gotten that part to work and to append the new feature to the original feature class.&amp;nbsp; Now I'm trying to add in some functionality so that if the user forgets to enter a project id for the point, an error message will be printed and then a subroutine is called so they can then enter the project id.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is my code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# Import arcpy module&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from arcpy import env&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#Set workspace&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;env.workspace = "L:/Projects/HMAP/EnvironmentalReview.gdb"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Script arguments&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Feature_Set = arcpy.GetParameterAsText(0)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if Feature_Set == '#' or not Feature_Set:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Feature_Set = "in_memory\\{C6359898-F410-49FF-96DD-0EB59C98516A}" # provide a default value if unspecified&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Local variables:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;##ProjectPoints = Feature_Set&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;##ProjectPoints__2_ = "ProjectPoints"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#define subroutine if user forgot to enter a project identifier&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;def subroutine1():&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; cur = arcpy.UpdateCursor(Feature_Set)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cur:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newVal = arcpy.GetParameterAsText(1)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; idValue = row.setValue(newVal)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.updateRow(row)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del cur, row&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#Check to make sure user entered a project identifier for point&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;cur = arcpy.SearchCursor(Feature_Set)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for row in cur:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; idValue = row.getValue("ProjectID")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if idValue == None:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("PLEASE ENTER A VALUE(S) FOR PROJECT ID")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; subroutine1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Append_management(Feature_Set, "ProjectPoints", "TEST", "", "")&amp;nbsp; # Process: Append&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I know I'm doing something wrong as far as the subroutine is concerned.&amp;nbsp; When I run the tool, it allows me to click the mouse to add a point.&amp;nbsp; Then I hit "o.k" on the interactive input window to simulate someone forgetting to input the project id.&amp;nbsp; Then the rest of the script starts to run but I get an error stating that "the name Feature_Set is not defined".&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I've tried reading all kinds of documentation on defining functions but I apparently still don't quite get it.&amp;nbsp; Can anyone help?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks much!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Amy Meehan&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;MDIFW&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Bangor, Maine&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Oct 2011 14:27:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-write-a-subroutine/m-p/560508#M43841</guid>
      <dc:creator>AmyMeehan</dc:creator>
      <dc:date>2011-10-26T14:27:20Z</dc:date>
    </item>
    <item>
      <title>Re: trying to write a subroutine</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-write-a-subroutine/m-p/560509#M43842</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I'm trying to write a subroutine for the first time and I'm getting very confused.&amp;nbsp; I'm trying to create a tool where users would interactively add a point to a feature class (using a feature set and appending it to the original feature class).&amp;nbsp; The tool uses the interactive feature input control.&amp;nbsp; So far I've gotten that part to work and to append the new feature to the original feature class.&amp;nbsp; Now I'm trying to add in some functionality so that if the user forgets to enter a project id for the point, an error message will be printed and then a subroutine is called so they can then enter the project id.&amp;nbsp; &lt;BR /&gt;&lt;BR /&gt;Here is my code:&lt;BR /&gt;# Import arcpy module&lt;BR /&gt;import arcpy&lt;BR /&gt;from arcpy import env&lt;BR /&gt;&lt;BR /&gt;#Set workspace&lt;BR /&gt;#&lt;BR /&gt;env.workspace = "L:/Projects/HMAP/EnvironmentalReview.gdb"&lt;BR /&gt;&lt;BR /&gt;# Script arguments&lt;BR /&gt;Feature_Set = arcpy.GetParameterAsText(0)&lt;BR /&gt;if Feature_Set == '#' or not Feature_Set:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Feature_Set = "in_memory\\{C6359898-F410-49FF-96DD-0EB59C98516A}" # provide a default value if unspecified&lt;BR /&gt;&lt;BR /&gt;# Local variables:&lt;BR /&gt;##ProjectPoints = Feature_Set&lt;BR /&gt;##ProjectPoints__2_ = "ProjectPoints"&lt;BR /&gt;&lt;BR /&gt;#define subroutine if user forgot to enter a project identifier&lt;BR /&gt;#&lt;BR /&gt;def subroutine1():&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; cur = arcpy.UpdateCursor(Feature_Set)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cur:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newVal = arcpy.GetParameterAsText(1)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; idValue = row.setValue(newVal)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.updateRow(row)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del cur, row&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;BR /&gt;&lt;BR /&gt;#Check to make sure user entered a project identifier for point&lt;BR /&gt;#&lt;BR /&gt;cur = arcpy.SearchCursor(Feature_Set)&lt;BR /&gt;for row in cur:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; idValue = row.getValue("ProjectID")&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if idValue == None:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("PLEASE ENTER A VALUE(S) FOR PROJECT ID")&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; subroutine1&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Append_management(Feature_Set, "ProjectPoints", "TEST", "", "")&amp;nbsp; # Process: Append&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I know I'm doing something wrong as far as the subroutine is concerned.&amp;nbsp; When I run the tool, it allows me to click the mouse to add a point.&amp;nbsp; Then I hit "o.k" on the interactive input window to simulate someone forgetting to input the project id.&amp;nbsp; Then the rest of the script starts to run but I get an error stating that "the name Feature_Set is not defined".&amp;nbsp; &lt;BR /&gt;&lt;BR /&gt;I've tried reading all kinds of documentation on defining functions but I apparently still don't quite get it.&amp;nbsp; Can anyone help?&lt;BR /&gt;&lt;BR /&gt;Thanks much!&lt;BR /&gt;&lt;BR /&gt;Amy Meehan&lt;BR /&gt;MDIFW&lt;BR /&gt;Bangor, Maine&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Amy-&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The subroutine (Python folks would just call this a 'function', which you have named 'subroutine1') looks okay to me. Except when you &lt;/SPAN&gt;&lt;SPAN style="text-decoration:underline;"&gt;call &lt;/SPAN&gt;&lt;SPAN&gt;a function, you need to use parentheses after it like this: &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;...
arcpy.AddError("PLEASE ENTER A VALUE(S) FOR PROJECT ID")
subroutine1()
...&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To see what's happening, go into a Python shell and do this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def my_function():
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "yes"

# type this first and see what happens
my_function()

#type this next and see what happens
my_function&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 00:11:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-write-a-subroutine/m-p/560509#M43842</guid>
      <dc:creator>PhilMorefield</dc:creator>
      <dc:date>2021-12-12T00:11:13Z</dc:date>
    </item>
    <item>
      <title>Re: trying to write a subroutine</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-write-a-subroutine/m-p/560510#M43843</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks!!&amp;nbsp; That worked!!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Amy&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Oct 2011 18:09:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-write-a-subroutine/m-p/560510#M43843</guid>
      <dc:creator>AmyMeehan</dc:creator>
      <dc:date>2011-10-26T18:09:38Z</dc:date>
    </item>
  </channel>
</rss>

