Select to view content in your preferred language

While loop won't end

445
2
05-13-2010 10:50 AM
KyleGallagher
New Contributor III
I can't get my while loop to terminate even though I'm using a counter variable.  I've included th relevant code and bolded the area of interest below.  I'm thinking it has to do with the numtins parameter.  The parameter is setup as an "Any Value" type, and I'm not sure if the code is reading it as a number or not.  For example, if I input the number 3 as the value of the numtins parameter, I would expect my output to be:

1
2
3

.....but instead, it is an infinite.  What am i missing here?

Thanks for the help!

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create(9.3)

# Check out any necessary licenses
gp.CheckOutExtension("spatial")
gp.CheckOutExtension("3D")

# Read Parameters...
dem_ft = sys.argv[1]
outfolder = sys.argv[2]
numtins = sys.argv[3]

counter = 1

while counter <= numtins:
    print counter
    counter += 1


NOTE:  THE TWO LINES IN THE WHILE LOOP ARE INDENTED, I CAN'T GET THEM TO SHOW UP THAT WAY IN THE THREAD.
0 Kudos
2 Replies
KevinHibma
Esri Regular Contributor
I'm not sure if you're running this as a script tool or through an IDE.

I'm pretty sure its getting confused with your numtins, it doesnt know that its an integer.

The following works for me (casting the input to an INT and passing in a value of say 5)

import sys, os

numtins = int(sys.argv[1])

counter = 1

print numtins
os.system("pause")

while counter <= numtins:
    print counter
    counter += 1
    


Otherwise just add in the pause statment and print out numtins (just to be sure what you're comparing)
0 Kudos
KyleGallagher
New Contributor III
Casting numtins to an int made it work.  Thanks!
0 Kudos