-1

Is there a reason my count equals zero, when it should be 29. I tried to initialize count inside my function but then it says its not defined.

shapefile = "Schools.shp"
work = r"c:\Scripts\Lab 6 Data"
facility = "HIGH SCHOOL"
count = 0

def numSchools(work, shapefile, facility):

    whereClause = "\"FACILITY\" = 'HIGH SCHOOL' " # where clause for high schools
    cursor = arcpy.SearchCursor("Schools.shp", facility)
    result = arcpy.GetCount_management(cursor)
    for result in cursor:
        count = int(result.getOutput(0))# Get the first value from the Result object
    return count

print ("The number of " + facility + " are:"), int(count) 

In its most basic form, this code returns the correct number of schools.

import arcpy

arcpy.env.workspace = r"c:\Scripts\Lab 6 Data"
result = arcpy.GetCount_management("Schools.shp")
count = int(result.getOutput(0)) 
print ("The number of rows in 'schools' is "),int(count)

However, Schools.shp is a file that contains 201 schools.

I need to go within that file to a field called 'facilities', then within that a get the number of 'high schools' (I know how to do this part). BUT I also need this code to work as a function (Which is what I am having trouble with).

A_Worthy
  • 65
  • 1
  • 2
  • 12
  • Can you include your error message? – dmlicht Jan 28 '17 at 19:26
  • 2
    You define a function, but never actually call it. Nothing you wrote inside the function matters. – jasonharper Jan 28 '17 at 19:26
  • @dmlicht NameError: name 'count' is not defined – A_Worthy Jan 28 '17 at 19:38
  • @A_Worthy i have copied and pasted your it does not give error! It gives 0. Do you mean it gives error after you call the method? – Md Sifatul Islam Jan 28 '17 at 19:44
  • @Md.SifatulIslam I think it is getting that from me saying count = 0. But it should be going into the school.shp file, within that file, there is a field called high school, that has 29 high schools listed. I am trying to return the number of high schools, which is 29. – A_Worthy Jan 28 '17 at 19:53
  • @A_Worthy would you check the answer I have tried for you? – Md Sifatul Islam Jan 28 '17 at 19:54
  • I have tried this code without using the function format and it works fine. But it is required that i use it as a function. – A_Worthy Jan 28 '17 at 19:58
  • 1
    @A_Worthy edit your question please and add the error message plus give a snap of your code(without funciton) that worked fine for you! We all can take a look and help I guess – Md Sifatul Islam Jan 28 '17 at 20:07
  • @A_Worthy did the error message have a line number? Without the line number, we don't know if your error is coming from your return statement or your print statement. If its coming from your return statement, your error could be from cursor being an empty list. – dmlicht Jan 28 '17 at 20:23
  • the error message was from the print statement – A_Worthy Jan 28 '17 at 20:35
  • For questions about ArcPy you may want to consider the [gis.se] Stack Exchange. – PolyGeo Jan 29 '17 at 19:04

2 Answers2

0

Though your question is not clearing I assume you are looking for this!

print ("The number of " + facility + " are:", int(count) ) 
Md Sifatul Islam
  • 846
  • 10
  • 28
-1
            searchCurs = arcpy.da.SearchCursor(shapefile, field, whereClause)
            row = searchCurs.next()
            for row in searchCurs:
                            value = row[0]
                            high_schools = [row[0] for row in arcpy.da.SearchCursor(shapefile, field, whereClause)]
                            count = len(high_schools)
                            print count
A_Worthy
  • 65
  • 1
  • 2
  • 12