 This video shows one possible solution to lesson three practice exercise B Which calculates the number of cities in the data set that have at least two park and ride facilities Using selections in ArcGIS It also uses an update cursor to update a field indicating whether the city has at least two park and rides The beginning of the script is very similar to practice exercise a lines four through eight are pretty familiar from that exercise In lines nine and ten. I'm setting up variables to refer to a couple of fields. I'm going to use Part of the exercise is to update a field called has two park and rides to either true or false So I'm setting up a variable for that In line ten. I'm setting up a real variable for the city FIPS code We're going to be querying each city one by one. So we need to have a field that has unique values for each city We could use a city name But that might be dangerous in the event that you've got a data set where two cities happen to have the same name So you use the FIPS code instead In lines 11 and 12 I'm setting up counters that I'm going to use to count the number of cities that have at least two park and rides and Then the total number of cities respectively In line 15 I'm starting out by making a feature layer of just the park and ride facilities This should be familiar from the previous exercise where the first parameter I pass in is the path to the park and ride data set and the second parameter is a name That I want to use for the feature layer object that I'm creating in line 18 I'm opening an update cursor on the city boundaries feature class So within this cursor, I'm going to loop through each city And select the number of park and ride facilities that occur in each city count them up So I'm going to be working with two fields in this cursor The city ID string field and the park and ride field This point it might be helpful to take a look at what we're doing conceptually in ArcGIS Pro The first step is to we're going to be selecting by attributes on the city boundaries Layer and we're going to be selecting based on the FIPS field Here I'll plug in a the value associated with Seattle Okay, I'll run that And that selects the city boundaries feature pertaining to Seattle And then with that feature selected we can use it in a select by location Where we are selecting park and ride features that intersect Features in city boundaries, and it's going to use just that selected feature And so there you see the park and rides within Seattle are selected And at that point I could count them up to see how many there are and basically we're going to be doing this same process For every city in the city boundaries feature class Move on to the next city do the processing for that city and so on so on As you might imagine that's a process that could take a few seconds to to complete So getting back to the script Here we are at line 18 we're creating this cursor which we give a name of city rose and Then we can loop through each city Returned by the cursor. So that's what we're doing online 19 with that for loop On line 21 we get the FIPS code for the city that's currently currently being looked at by the cursor We're using zero inside the square brackets here because that's the index position of the City ID string field that we put in the tuple when we created the update cursor That was the first item in the tuple. So it has an index position of zero On line 22 We're setting up a string to query for that particular city So we need the field name equals city ID The city ID has to be in single quotes because it's a string So we use string concatenation to plug the field name and the city ID into a string expression Once we have that string we can make a feature layer That has just that one city in it We the way we do that is in line 25 We call in the make feature layer tool again, but instead of passing two parameters here, we're going to pass three The first two are going to be the same as before Plug in the path to the data set We assign a name for the feature layer object and in this case we're going to call that feature layer current city layer But then the third parameter is that query string that we just created on line 22 And that's going to cause our feature layer to have just one city in it So now we're going to do the location selection on line 30 To select just the parking rides that are contained by that one city The parameters here should be pretty intuitive if you have that understanding of how the selection is working Where just one city boundary is selecting a subset of parking rides so at that point you can call the get count tool and Figure out how many parking rides were just selected So that's what's going on on line 33 remember on line 34 That you have to use zero and square brackets to get at the actual count value Which is a string and then use the int function to convert the string into an integer We saw that in practice exercise a So in the end what you have in line 34 is that variable called num selected park and ride That's the number of park and ride facilities that fell within whatever the current city is in the cursor So line 37 is doing a check to see if that number is greater than or equal to 2 And if it is then we're going to assign a value of true to that has park has to park and ride Field So that's where we using our cursor go to the first item in the tuple Actually the second item, but it has an index position of one and we set that equal to true And we don't want to forget to call the update row method So that's so that the change we may just made get supplied to the data set Then the next thing we do is we want to update our counter variable We want to add one to the counter To account for that That city that that does have to park and rides Give in mind that we're doing this selection of parking rides for every city in the city boundaries feature class On each pass through the loop. We want to create the current city layer feature layer Each time having referred to a different city. We can't create a Feature layer that already exists from the previous iteration of loop So associated with the try block. We have a finally block That Deletes the current city layer feature layer We also increment the number of cities we've counted by one in this finally block after the loop We want to clean up the other objects that have locks on our data that means using the delete tool On the park and ride layer feature layer and a python del statement to kill the objects held in the city and city rows variables so notice there's a lot of levels of indentation here and Remember that getting this this indentation right is a critical part of coding in python this try block needs to be Indented with respect to line 19 so that it's considered part of the loop the finally block Needs to be Intended at the same level as the try block in order to be associated with that try block So after doing all the cleanup now, we're going to calculate the percentage of cities that have at least two parking rides Now you can't divide by zero. So we're doing a check on line 56 for that But if everything checks out, okay Which it would unless you have a major problem with your data set we go ahead in line 57 And run some division to divide the number of cities with two parking rides by the total number of cities and multiply by a hundred So lines 5960 we have a little bit of error handling in the event that you Don't have any cities Otherwise, we'll calculate the percentage and report it on lines 57 and 58