<?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: Script stops unexpectedly. in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/script-stops-unexpectedly/m-p/378598#M29873</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;So now you have a KEYERROR, meaning you're having problems accessing the requested key (the key doesn't exist).&lt;BR /&gt;&lt;BR /&gt;This is the error printed:&lt;BR /&gt;&lt;BR /&gt;rtNHbt = rtOFDict[(1,'VALUE_0')]&lt;BR /&gt;KeyError: (1, 'VALUE_0')&lt;BR /&gt;&lt;BR /&gt;The key is of course (1, 'VALUE_0') and apparently for this case it does not exist.&amp;nbsp; Maybe this will help you:&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://wiki.python.org/moin/KeyError" rel="nofollow noopener noreferrer" target="_blank"&gt;https://wiki.python.org/moin/KeyError&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;You can think what could possibly cause this missing key and correct it or otherwise trap the error, or if you like have a default value returned.&lt;BR /&gt;&lt;BR /&gt;Enjoy,&lt;BR /&gt;Wayne&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;So as a very brief example using a default value of 0 (zero):&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt; # dictionary of only 2 keys, 'this' and 'that':
&amp;gt;&amp;gt;&amp;gt; dict = {'this':123, 'that':456}

&amp;gt;&amp;gt;&amp;gt; # looking up an exising key, 'this':
&amp;gt;&amp;gt;&amp;gt; dict['this']
123

&amp;gt;&amp;gt;&amp;gt; # setting up a var to use if a key doesn't exist, for example 0 (zero):
&amp;gt;&amp;gt;&amp;gt; someDefaultVal = 0

&amp;gt;&amp;gt;&amp;gt; # using 'get' to retrieve the default assignment in the event of a missing key:
&amp;gt;&amp;gt;&amp;gt; dict.get('theOther', someDefaultVal)
0
&amp;gt;&amp;gt;&amp;gt; # of course, using 'get' to retrieve an existing assignment returns the proper val:
&amp;gt;&amp;gt;&amp;gt; dict.get('that', someDefaultVal)
456
&amp;gt;&amp;gt;&amp;gt; 
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;Notice I am not saying this is the best way to handle this - the best way is to identify and handle the error...this is only illustrating an option to 'bypass' the error and return 0.&amp;nbsp; You'd have to be careful that the other vals in your divisor are not zero or you'll introduce a new error, division by zero.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I think this is the answer!&amp;nbsp;&amp;nbsp; I believe an odd case, Value 0 is not returned in the summary table because no 0 values were found in the buffer.&amp;nbsp; I will test this and see.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 17:29:18 GMT</pubDate>
    <dc:creator>AliciaMein</dc:creator>
    <dc:date>2021-12-11T17:29:18Z</dc:date>
    <item>
      <title>Script stops unexpectedly.</title>
      <link>https://community.esri.com/t5/python-questions/script-stops-unexpectedly/m-p/378594#M29869</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="plain" name="code"&gt;#Script that computes the Percent forsted area from a landcover raster and #county zone data.&amp;nbsp; Then finds the buffer distance required around sampling #routes, so the buffer frstd percent matches the county frstd percent.&amp;nbsp; #BufferDist attribute of the route feature class are changed to reflect the #values found by the script.&amp;nbsp; !! The BufferDist attribute will be changed. #Set environment and declare varibles # arcpy.env.overwriteOutput = True arcpy.env.workspace = "C:/CustomTools/DeerSurveyRoutes/rtsScratch.gdb"&amp;nbsp; coPath = "C:/CustomTools/DeerSurveyRoutes/RtsAnlysVectors.gdb/County" rasPath = "C:/CustomTools/DeerSurveyRoutes/RtsAnlysRasters.gdb/WVUReclass" opnFrstdRas = arcpy.Raster("C:/CustomTools/DeerSurveyRoutes/RtsAnlysRasters.gdb/WVUReclass") rasCellSz = (opnFrstdRas.meanCellHeight + opnFrstdRas.meanCellWidth) / 2 rtsPath = "C:/CustomTools/DeerSurveyRoutes/RtsAnlysVectors.gdb/SmplRts2012Edited4CountyAnalysis"&amp;nbsp; #Set environemt and create variables for County area tabulation # arcpy.CheckOutExtension("Spatial") arcpy.env.snapRaster = rasPath znFld = "CoZoneName" clsFld = "Value" outTble = "CoTbleOpenFrst"&amp;nbsp; # Tabulate area of each class in raster and set up dictionary to hold values in table. # arcpy.sa.TabulateArea(coPath,znFld,opnFrstdRas,clsFld,outTble,rasCellSz) coOFDict = dict([((r.COZONENAME, f.name), r.getValue(f.name)) for f in arcpy.ListFields(outTble) for r in arcpy.SearchCursor(outTble)]) del r, f&amp;nbsp; #Create dictionary of Rts Containing the CoZoneName, RouteID and BufferDist rtDict = dict([((r.RouteID, f.name), r.getValue(f.name)) for f in arcpy.ListFields(rtsPath) for r in arcpy.SearchCursor(rtsPath)]) del r, f&amp;nbsp; #Loop to find buffer distance of each route that will contain the same forested percent as the county&amp;nbsp; return BuffDist # buffCur = arcpy.SearchCursor(rtsPath) #row = buffCur.next()&amp;nbsp; (not needed) for row in buffCur: &amp;nbsp;&amp;nbsp;&amp;nbsp; buffDist = 402 &amp;nbsp;&amp;nbsp;&amp;nbsp; frstPrcnt = 0 &amp;nbsp;&amp;nbsp;&amp;nbsp; frstd = coOFDict[(row.getValue(znFld),'VALUE_2')] &amp;nbsp;&amp;nbsp;&amp;nbsp; opn = coOFDict[(row.getValue(znFld),'VALUE_1')] &amp;nbsp;&amp;nbsp;&amp;nbsp; nHbt = coOFDict[(row.getValue(znFld),'VALUE_0')] &amp;nbsp;&amp;nbsp;&amp;nbsp; lpChkVal = frstd / (frstd + opn + nHbt) &amp;nbsp;&amp;nbsp;&amp;nbsp; minLpChkVal = lpChkVal - (lpChkVal * .05) &amp;nbsp;&amp;nbsp;&amp;nbsp; maxLpChkVal = lpChkVal + (lpChkVal * .05) &amp;nbsp;&amp;nbsp;&amp;nbsp; x = 0&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; #Set up feature layer for Buffer analysis &amp;nbsp;&amp;nbsp;&amp;nbsp; #thisRtID = row.getValue('RouteID') &amp;nbsp;&amp;nbsp;&amp;nbsp; #arcpy.MakeFeatureLayer_management(rtsPath, "selectedLine", '"RouteID" = \'' + thisRtID + '\'')&amp;nbsp; (good where clause example - do not delete)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; while frstPrcnt &amp;lt; minLpChkVal or frstPrcnt &amp;gt; maxLpChkVal:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Buffer_analysis(row.shape, "lineBuffer", buffDist,"FULL","ROUND","ALL") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.sa.TabulateArea("lineBuffer","OBJECTID",rasPath,clsFld,"BufferOFTble",rasCellSz) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rtOFDict = dict([((r.OBJECTID, f.name), r.getValue(f.name)) for f in arcpy.ListFields("BufferOFTble") for r in arcpy.SearchCursor("BufferOFTble")]) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del r, f &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rtFrstd = rtOFDict[(1,'VALUE_2')] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rtOpn = rtOFDict[(1,'VALUE_1')] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rtNHbt = rtOFDict[(1,'VALUE_0')] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; frstPrcnt = rtFrstd / (rtFrstd + rtOpn + rtNHbt) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newBuffVal = buffDist &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x += 1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if x == 30: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rtDict[(row.getValue('RouteID'), 'BufferDist')] = 1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; buffDist = buffDist + (x * 5 * rasCellSz) &amp;nbsp;&amp;nbsp;&amp;nbsp; rtDict[(row.getValue('RouteID'), 'BufferDist')] = newBuffVal&amp;nbsp; #Create Cursor to iterate through rts and dictionary to update buffer distance. # updtCur = arcpy.UpdateCursor(rtsPath)&amp;nbsp; for rows in updtCur: &amp;nbsp;&amp;nbsp;&amp;nbsp; dist = rtDict[(rows.getValue('RouteID'), 'BufferDist')] &amp;nbsp;&amp;nbsp;&amp;nbsp; rows.setValue('BufferDist', dist) &amp;nbsp;&amp;nbsp;&amp;nbsp; updtCur.updateRow(rows) del rows, updtCur, row, buffCur&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I suspect that this sort of create, delete and overwrite is not optimal.&amp;nbsp; I am learning and would appreciate understanding what is going on.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I also realize that I might be better of passing the entire feature class and working through buffering all features instead of just one, but right now I just want to understand some concepts.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a feature class containing only 140ish features.&amp;nbsp; I hoped this would not be to large a burden for my computer.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I developed and tested with two routes just to make sure everything was claculated and completed.&amp;nbsp; When I took the script and ran it against a file geodatabase on a network the script immediately returned :&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Runtime error &amp;lt;type 'exceptions.NameError'&amp;gt;: name 'r' is not defined&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is the variable used in the dictionary to populate the key.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I ran the script from my C again it processed fairly quickly and returned the same error after processing 130ish records.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is Why to broad a question?&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have already changed the end type in the buffer tool from flat to round, because the buffers where eating themselves from the inside out.&amp;nbsp; I am going to change variable r to unique values in each dict and Id will also implement error checking.&amp;nbsp; But as is, does anybody have any idea why the script doesnt recognize r?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I appreciate all the help I have recieved.&amp;nbsp; Any comments are welcome.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Alicia&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 18 Oct 2013 18:12:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-stops-unexpectedly/m-p/378594#M29869</guid>
      <dc:creator>AliciaMein</dc:creator>
      <dc:date>2013-10-18T18:12:10Z</dc:date>
    </item>
    <item>
      <title>Re: Script stops unexpectedly.</title>
      <link>https://community.esri.com/t5/python-questions/script-stops-unexpectedly/m-p/378595#M29870</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;What happens if you take out the following line?:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;del r, f&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;NameError exception usually refers to a var ref that is unkown, either not properly initiated or already terminated...in your case, I think it's the way you initiated that python has already 'garbage collected' it and thus you don't need to use the 'del' command.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As for further comment on the efficiency of your script, let's just see how it runs 1st....leaving the testing to you.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 20 Oct 2013 17:09:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-stops-unexpectedly/m-p/378595#M29870</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-10-20T17:09:48Z</dc:date>
    </item>
    <item>
      <title>Re: Script stops unexpectedly.</title>
      <link>https://community.esri.com/t5/python-questions/script-stops-unexpectedly/m-p/378596#M29871</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;What happens if you take out the following line?:&lt;BR /&gt;del r, f&lt;BR /&gt;&lt;BR /&gt;NameError exception usually refers to a var ref that is unkown, either not properly initiated or already terminated...in your case, I think it's the way you initiated that python has already 'garbage collected' it and thus you don't need to use the 'del' command.&lt;BR /&gt;&lt;BR /&gt;As for further comment on the efficiency of your script, let's just see how it runs 1st....leaving the testing to you.&lt;BR /&gt;&lt;BR /&gt;Enjoy,&lt;BR /&gt;Wayne&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for the input.&amp;nbsp; I tried to run many different ways, and consistently stops.&amp;nbsp; Then I changed my code a slightly and ran from the development environment (outside acrmap)&amp;nbsp; and recieved the following.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "C:.........StepAnalysisTestCrum4.py", line 54, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; rtNHbt = rtOFDict[(1,'VALUE_0')]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;KeyError: (1, 'VALUE_0')&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The error was returned after looping just shy of 4000 times.&amp;nbsp; processing 76 out of 143 lines.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My new code is very similar to the intiial.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
#Set environment and declare varibles
#
import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:/CustomTools/DeerSurveyRoutes/RtsScratch.gdb"

coPath = "C:/CustomTools/DeerSurveyRoutes/RtsAnlysVectors.gdb/County"
rasPath = "C:/CustomTools/DeerSurveyRoutes/RtsAnlysRasters.gdb/WVUReclass"
opnFrstdRas = arcpy.Raster("C:/CustomTools/DeerSurveyRoutes/RtsAnlysRasters.gdb/WVUReclass")
rasCellSz = (opnFrstdRas.meanCellHeight + opnFrstdRas.meanCellWidth) / 2
rtsPath = "C:/CustomTools/DeerSurveyRoutes/RtsAnlysVectors.gdb/SmplRts2012Edited4CountyAnalysis"

#Set environment and create variables for County area tabulation
#
arcpy.CheckOutExtension("Spatial")
arcpy.env.snapRaster = rasPath
znFld = "CoZoneName"
clsFld = "Value"
outTble = "CoTbleOpenFrst"

# Tabulate area of each class in raster and set up dictionary to hold values in table.
#
arcpy.sa.TabulateArea(coPath,znFld,opnFrstdRas,clsFld,outTble,rasCellSz)
coOFDict = dict([((coR.COZONENAME, coF.name),coR.getValue(coF.name)) for coF in arcpy.ListFields(outTble) for coR in arcpy.SearchCursor(outTble)])
del coR, coF

#Create dictionary of Rts containing the CoZoneName, RouteID and BufferDist
rtDict = dict([((rtR.RouteID, rtF.name),rtR.getValue(rtF.name)) for rtF in arcpy.ListFields(rtsPath) for rtR in arcpy.SearchCursor(rtsPath)])
del rtR, rtF

#Loop to find buffer distance of each route that will contain the same forested percent as the county&amp;nbsp; return BuffDist
#
buffCur = arcpy.SearchCursor(rtsPath)
#row = buffCur.next()
for row in buffCur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; buffDist = row.getValue('Yds2Meters')
&amp;nbsp;&amp;nbsp;&amp;nbsp; frstPrcnt = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; frstd = coOFDict[(row.getValue(znFld),'VALUE_2')]
&amp;nbsp;&amp;nbsp;&amp;nbsp; opn = coOFDict[(row.getValue(znFld),'VALUE_1')]
&amp;nbsp;&amp;nbsp;&amp;nbsp; nHbt = coOFDict[(row.getValue(znFld),'VALUE_0')]
&amp;nbsp;&amp;nbsp;&amp;nbsp; lpChkVal = frstd / (frstd + opn + nHbt)
&amp;nbsp;&amp;nbsp;&amp;nbsp; minLpChkVal = lpChkVal - (lpChkVal * .05)
&amp;nbsp;&amp;nbsp;&amp;nbsp; maxLpChkVal = lpChkVal + (lpChkVal * .05)
&amp;nbsp;&amp;nbsp;&amp;nbsp; x = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; listFrstPrcnt = []

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Run buffer and table against initial value to set up comparison for min/max values
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Buffer_analysis(row.shape, "lineBuffer", buffDist,"FULL","ROUND","ALL")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.sa.TabulateArea("lineBuffer","OBJECTID",rasPath,clsFld,"BufferOFTble",rasCellSz)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; rtOFDict = dict([((r.OBJECTID, f.name),r.getValue(f.name)) for f in arcpy.ListFields("BufferOFTble") for r in arcpy.SearchCursor("BufferOFTble")])
&amp;nbsp;&amp;nbsp;&amp;nbsp; del r, f
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; rtFrstd = rtOFDict[(1,'VALUE_2')]
&amp;nbsp;&amp;nbsp;&amp;nbsp; rtOpn = rtOFDict[(1,'VALUE_1')]
&amp;nbsp;&amp;nbsp;&amp;nbsp; rtNHbt = rtOFDict[(1,'VALUE_0')]
&amp;nbsp;&amp;nbsp;&amp;nbsp; frstPrcnt = rtFrstd / (rtFrstd + rtOpn + rtNHbt)

&amp;nbsp;&amp;nbsp;&amp;nbsp; listVal = frstPrcnt
&amp;nbsp;&amp;nbsp;&amp;nbsp; listFrstPrcnt.append(listVal)
&amp;nbsp;&amp;nbsp;&amp;nbsp; newBuffVal = buffDist
&amp;nbsp;&amp;nbsp;&amp;nbsp; buffDist = 402
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; #Set up feature layer for Buffer analysis
&amp;nbsp;&amp;nbsp;&amp;nbsp; #thisRtID = row.getValue('RouteID')
&amp;nbsp;&amp;nbsp;&amp;nbsp; #arcpy.MakeFeatureLayer_management(rtsPath, "selectedLine", '"RouteID" = \'' + thisRtID + '\'')

&amp;nbsp;&amp;nbsp;&amp;nbsp; while frstPrcnt &amp;lt; minLpChkVal or frstPrcnt &amp;gt; maxLpChkVal:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Buffer_analysis(row.shape, "lineBuffer", buffDist,"FULL","ROUND","ALL")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.sa.TabulateArea("lineBuffer","OBJECTID",rasPath,clsFld,"BufferOFTble",rasCellSz)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rtOF2Dict = dict([((ofR.OBJECTID, ofF.name),ofR.getValue(ofF.name)) for ofF in arcpy.ListFields("BufferOFTble") for ofR in arcpy.SearchCursor("BufferOFTble")])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rtFrstd = rtOF2Dict[(1,'VALUE_2')]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rtOpn = rtOF2Dict[(1,'VALUE_1')]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rtNHbt = rtOF2Dict[(1,'VALUE_0')]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; frstPrcnt = rtFrstd / (rtFrstd + rtOpn + rtNHbt)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; listVal = frstPrcnt
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; listFrstPrcnt.append(listVal)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newBuffVal = buffDist
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x += 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; buffDist = 402 + (x * 5 * rasCellSz)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if min(listFrstPrcnt) &amp;lt; minLpChkVal and max(listFrstPrcnt) &amp;gt; maxLpChkVal:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break
&amp;nbsp;&amp;nbsp;&amp;nbsp; rtDict[(row.getValue('RouteID'), 'BufferDist')] = newBuffVal
&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.getValue('RouteID')
#Create Cursor to iterate through rts and dictionary to update buffer distance.
#
updtCur = arcpy.UpdateCursor(rtsPath)

for rows in updtCur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; dist = rtDict[(rows.getValue('RouteID'), 'BufferDist')]
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.setValue('BufferDist', dist)
&amp;nbsp;&amp;nbsp;&amp;nbsp; updtCur.updateRow(rows)
del rows, updtCur, row, buffCur, ofR, ofF
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:29:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-stops-unexpectedly/m-p/378596#M29871</guid>
      <dc:creator>AliciaMein</dc:creator>
      <dc:date>2021-12-11T17:29:15Z</dc:date>
    </item>
    <item>
      <title>Re: Script stops unexpectedly.</title>
      <link>https://community.esri.com/t5/python-questions/script-stops-unexpectedly/m-p/378597#M29872</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;So now you have a KEYERROR, meaning you're having problems accessing the requested key (the key doesn't exist).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is the error printed:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;rtNHbt = rtOFDict[(1,'VALUE_0')]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;KeyError: (1, 'VALUE_0')&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The key is of course (1, 'VALUE_0') and apparently for this case it does not exist.&amp;nbsp; Maybe this will help you:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A class="jive-link-external-small" href="https://wiki.python.org/moin/KeyError" rel="nofollow" target="_blank"&gt;https://wiki.python.org/moin/KeyError&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can think what could possibly cause this missing key and correct it or otherwise trap the error, or if you like have a default value returned.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So as a very brief example using a default value of 0 (zero):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;&amp;gt;&amp;gt;&amp;gt; # dictionary of only 2 keys, 'this' and 'that': &amp;gt;&amp;gt;&amp;gt; dict = {'this':123, 'that':456}&amp;nbsp; &amp;gt;&amp;gt;&amp;gt; # looking up an exising key, 'this': &amp;gt;&amp;gt;&amp;gt; dict['this'] 123&amp;nbsp; &amp;gt;&amp;gt;&amp;gt; # setting up a var to use if a key doesn't exist, for example 0 (zero): &amp;gt;&amp;gt;&amp;gt; someDefaultVal = 0&amp;nbsp; &amp;gt;&amp;gt;&amp;gt; # using 'get' to retrieve the default assignment in the event of a missing key: &amp;gt;&amp;gt;&amp;gt; dict.get('theOther', someDefaultVal) 0 &amp;gt;&amp;gt;&amp;gt; # of course, using 'get' to retrieve an existing assignment returns the proper val: &amp;gt;&amp;gt;&amp;gt; dict.get('that', someDefaultVal) 456 &amp;gt;&amp;gt;&amp;gt; &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Notice I am not saying this is the best way to handle this - the best way is to identify and handle the error...this is only illustrating an option to 'bypass' the error and return 0.&amp;nbsp; You'd have to be careful that the other vals in your divisor are not zero or you'll introduce a new error, division by zero.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Oct 2013 18:18:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-stops-unexpectedly/m-p/378597#M29872</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-10-28T18:18:09Z</dc:date>
    </item>
    <item>
      <title>Re: Script stops unexpectedly.</title>
      <link>https://community.esri.com/t5/python-questions/script-stops-unexpectedly/m-p/378598#M29873</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;So now you have a KEYERROR, meaning you're having problems accessing the requested key (the key doesn't exist).&lt;BR /&gt;&lt;BR /&gt;This is the error printed:&lt;BR /&gt;&lt;BR /&gt;rtNHbt = rtOFDict[(1,'VALUE_0')]&lt;BR /&gt;KeyError: (1, 'VALUE_0')&lt;BR /&gt;&lt;BR /&gt;The key is of course (1, 'VALUE_0') and apparently for this case it does not exist.&amp;nbsp; Maybe this will help you:&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://wiki.python.org/moin/KeyError" rel="nofollow noopener noreferrer" target="_blank"&gt;https://wiki.python.org/moin/KeyError&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;You can think what could possibly cause this missing key and correct it or otherwise trap the error, or if you like have a default value returned.&lt;BR /&gt;&lt;BR /&gt;Enjoy,&lt;BR /&gt;Wayne&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;So as a very brief example using a default value of 0 (zero):&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt; # dictionary of only 2 keys, 'this' and 'that':
&amp;gt;&amp;gt;&amp;gt; dict = {'this':123, 'that':456}

&amp;gt;&amp;gt;&amp;gt; # looking up an exising key, 'this':
&amp;gt;&amp;gt;&amp;gt; dict['this']
123

&amp;gt;&amp;gt;&amp;gt; # setting up a var to use if a key doesn't exist, for example 0 (zero):
&amp;gt;&amp;gt;&amp;gt; someDefaultVal = 0

&amp;gt;&amp;gt;&amp;gt; # using 'get' to retrieve the default assignment in the event of a missing key:
&amp;gt;&amp;gt;&amp;gt; dict.get('theOther', someDefaultVal)
0
&amp;gt;&amp;gt;&amp;gt; # of course, using 'get' to retrieve an existing assignment returns the proper val:
&amp;gt;&amp;gt;&amp;gt; dict.get('that', someDefaultVal)
456
&amp;gt;&amp;gt;&amp;gt; 
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;Notice I am not saying this is the best way to handle this - the best way is to identify and handle the error...this is only illustrating an option to 'bypass' the error and return 0.&amp;nbsp; You'd have to be careful that the other vals in your divisor are not zero or you'll introduce a new error, division by zero.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I think this is the answer!&amp;nbsp;&amp;nbsp; I believe an odd case, Value 0 is not returned in the summary table because no 0 values were found in the buffer.&amp;nbsp; I will test this and see.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:29:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-stops-unexpectedly/m-p/378598#M29873</guid>
      <dc:creator>AliciaMein</dc:creator>
      <dc:date>2021-12-11T17:29:18Z</dc:date>
    </item>
  </channel>
</rss>

