Python Conditional Statement not Working

3585
5
Jump to solution
02-12-2016 08:53 AM
RachelAlbritton
Occasional Contributor III

This should be pretty basic, but for the life of me I can not figure out why this conditional statement doesn't function correctly.

I'm using the count tool to count the # of selected records. If the count = 0, then the user should just get a message that says "No Issues were logged during this walk through." but if the count is greater than 0, the script is set to do an additional analysis.

My issue is when the count is greater then 0 - the script does what it is suppose to.

When the count is equal to zero the script runs without any error messages, however, fails to give me the output message that it is programmed to. Can anyone tell me why?

I thought it may have been the order in which I programmed the evaluation, but I got the same results either way. I left both

sets in the code, with one commented out so you can see what I've tried with the same results.

I'm in ArcGIS 10.3.1 and using Python 2.7.8

Thanks for the feedback.

#Count number of records selected
count = arcpy.GetCount_management(ViewTable)
print str(count) +" issues were logged\n"

if count > 0:
    sc1 = arcpy.SearchCursor(ViewTable)
    for row in sc1:
        infile.write("Stop "+str(row.Stop_Num)+": "+(row.Issue)+"\n\n")
        print "Stop "+str(row.Stop_Num)+": "+(row.Issue)+"\n\n"
        del row
    del sc1

else:
    
    infile.write("No Issues were logged during this walk through.\n")
    print "No Issues were logged during this walk through."
    
##if count == 0:
##    #infile.write("No Issues were logged during this walk through.\n")
##    print "No Issues were logged during this walk through."

##else:
##    sc1 = arcpy.SearchCursor(ViewTable)
##    for row in sc1:
##        infile.write("Stop "+str(row.Stop_Num)+": "+(row.Issue)+"\n\n")
##        print "Stop "+str(row.Stop_Num)+": "+(row.Issue)+"\n\n"
##        del row
##    del sc1


1 Solution

Accepted Solutions
Kathleen_Crombez
Occasional Contributor III

I think the problem is that arcpy.GetCount_management(ViewTable) returns an object.

So count will never equal 0.

You can test this by doing...

count = arcpy.GetCount_management(ViewTable) 

print type(count)

To get the integer value of the count object do this...

result = arcpy.GetCount_management(ViewTable)

count = int(result.getOutput(0))

View solution in original post

5 Replies
JamesCrandall
MVP Frequent Contributor

Just as a quick test, I'd comment out the infile.write just to see if the print statement is invoked in the else part of the condition.  If that's the case, the problem isn't with your conditional statement, it's with that .write line.

RachelAlbritton
Occasional Contributor III

Thanks James - I should have stated that I have tried that, and it still gave me the same scenario. I also stripped the entire analysis out of the if/else statement so that :

if count == 0:

print somthing

else:

print something else

When I do this, and count is equal to 0, it prints the message for the else statement.

0 Kudos
Kathleen_Crombez
Occasional Contributor III

I think the problem is that arcpy.GetCount_management(ViewTable) returns an object.

So count will never equal 0.

You can test this by doing...

count = arcpy.GetCount_management(ViewTable) 

print type(count)

To get the integer value of the count object do this...

result = arcpy.GetCount_management(ViewTable)

count = int(result.getOutput(0))

RachelAlbritton
Occasional Contributor III

Thanks Kathleen! That's exactly what the issue was.

0 Kudos
ZianChoy
Occasional Contributor

In ArcGIS Server 10.7.1, if you compare the object ("if result > 0") then sometimes, the boolean condition will return True and sometimes it returns False. Hope this helps anyone else wondering why their script breaks once every few hours.

0 Kudos