 This video is going to describe one possible solution for Lesson 4 Practice Exercise B where you're reading the names of soccer teams in Buenos Aires, looking at their scores, and then compiling a report about the top number of goals scored by each team over the course of the matches covered in the file. In order to maintain all this information, it's helpful to use dictionary, which is a way of storing data in the computer's memory based on key value pairs. So the key in this case will be the name of the team, and the value will be the maximum number of goals found from that team as we read the file line by line. Now this file is sort of like a comma separated value file, although the delimiter in our case is a space. The file does have a header, which we're going to use to pull out information. The header is organized in terms of winner, winner goals, loser, and loser goals, although really there are some ties in here, so we might say first score and second score rather than winner and loser. For our purposes, it doesn't matter who won or lost because the maximum number of goals might have come during a loss or a tie also. So this solution is a little more complex than some of the other practice exercises. It involves a function which you can see beginning in line nine. We're not going to talk about the function just yet. We'll come back to it later. Right now we're going to start with line 23 here, where we import the Python CSV module. Now there's nothing in this script that uses ArcGIS or geometries or anything like that. So I don't import ArcPy, but you will have to do that in project four, where you're going to be using a combination of techniques used here, along with ArcGIS geometries, and really putting everything together from both the practice exercises. In line 26, I set up a variable representing the path to the scores text file, and then in line 27 I actually open up that file. The file is opened in read mode. There's no opening mode parameter supplied here, but if you don't supply one read mode is the default. Line 29, we create the CSV reader object, which you should be familiar with from the other examples in the lesson and the other practice exercise. The one thing that's different here is the second parameter. We can specify the delimiter using the syntax delimiter equals, and in this case we plug in a space character. This file does have a header and in line 30 we read that header, and then we figure out the index positions of all the columns in the file. That's what's going on in lines 32 through 35. Now we know the positions of the columns so we could plug in hard coded 0, 1, 2, and 3 instead. But writing the code the way we have here makes the script a little more flexible in the event that the column were somehow got switched around. In line 39 we're going to create a blank dictionary to keep track of each team and the maximum number of goals that they've scored. We're going to refer to that dictionary frequently as we read through the file. In line 41 we begin a loop that actually starts reading the file row by row after the header. And so lines 44 through 47 are pulling out those four pieces of information, the two team names, and the number of goals that each scored. Note that the end function is used to convert the number of goals in string format to integers. Now when we get a team name and a number of goals we need to check it against our dictionary to see if the number of goals scored is greater than that team's max that we've found so far. And we need to do this check for both the winner and the loser. To avoid repeating code this is a good situation to employ a function because we're going to be using the same logic for both the winning team and the losing team. Why not write the code just once in a function. So in lines 50 and 53 you'll see that I'm invoking a function called check goals and I passed to the function three things. The team name, the number of goals, and the dictionary. The function we saw briefly earlier, that's the find up here. Line nine defines a function called check goals, and I create variables here for the three things that the function needs to do its job. The name of the team, the number of goals, and the dictionary. Line 11 performs a check to see is the team already in the dictionary. If it is, then we need to look at the number of goals that have been scored that have been stored for the team in the dictionary and check it against the score that was passed into the function. To see if that maximum number of goals has to be updated. So line 13, that's where that check is occurring. And if indeed the number of goals passed into the function is greater than the maximum that we've run into so far, then in line 14. That's where we update the team's entry in the dictionary, setting it equal to what's in the goals variable. If the number of goals passed into the function is not greater than our maximum, then we don't want to do anything. And that's what's happening on line 16. The pass keyword essentially says don't do anything here. And we really could eliminate this else clause altogether if we prefer. Now, if the team has never been read before, and it doesn't have an entry in the dictionary, then we're going to jump down to line 18, which says to add the team to the dictionary, setting its number of goals equal to whatever was passed into the function. Now, to get a better feel for how this script works. It makes sense to look at it in debugging using the debugging tools. So I'm going to insert a breakpoint here on the first line inside the function. And I'm going to run the script up until the first time this function gets called. And over here, I've got the variable explorer tab open so that I can watch what's happening with my variables as I run the script. So I hit the debug file button, which if you have a breakpoint in your script, it's going to run the script to the breakpoint. So right now the script execution is paused after the first call to the goals, the check goals function. And looking at my variables, I can see that the row variable holds the list of values from the first game, the file. The variables that I really want to focus on, though, are the ones that are defined as part of the function, the dictionary variable goals and the team variable. So on this first call to the function, we've passed in Boca as the team to as the number of goals. And right now we have an empty dictionary that we passed in. So when evaluating line 11, we would expect spider to jump down to line 19, because the team passed in doesn't have a key in the dictionary yet. So what we would want to happen is to add that team to the dictionary and in fact that's what's going to happen. So let me step through the code now with F10. And if you watch the variable explorer window, you should see that the dictionary will now get a team added to it. Boca with a number of goals of two. All right. And so then I'm going to click the continue button, which is going to pass, you know, continue executing the code, which we come back to the main body of the script, which then makes another call to check goals this time sending the losing teams information. And so now we see that the team is independent a number of goals is one. And so this is another new team that's not in the dictionary yet. So we would expect once again, the script execution to jump down here to the else clause, add the team to the dictionary, which in fact is what we see. So I'm going to click the continue button again. And there's the next game has a couple of more teams that aren't in the dictionary yet. So I'm just going to skip through them and add them to the dictionary. You see that they're now added. And at this point, we're we've come into check goals again. And the team name is river. And we actually already have that team in the dictionary. So this is a good moment to step through the code. Again, line by line to see what happens when we already have a team in the dictionary. So we would expect the control to jump down into this block here. And to check, does the number of goals passed into the function. Is it more than the number of goals that's in the dictionary for that team already. We would expect it to be that expression to evaluate to true, because the goals past that is to the number of goals held in the dictionary for that team is just zero. So I'll hit F 10. And again, and we see that it does in fact, evaluate to true we jump in here to line 14. And if you watch closely, you should see that reversed entry in the dictionary updates from zero to two. And it does. Okay, so we could continue stepping through the script. But hopefully you get a feel for how the logic plays out now. As you're working on project four. You're going to be working with dictionaries in this manner, please keep the debugger open and watch what's happening. And hopefully you'll be able to tell if your dictionary is being updated in the way that you expect. So to finish out the script, coming back to the main body. This for loop is complete. We will have populated our dictionary. And then we iterate through the dictionary goals dictionary with a for loop. And that begins on line 56. In this case key represents a key in the dictionary. And in line 57, we print out that key, followed by a colon and a space. And then we print the associated value for that key. If you want to pull a value out of a dictionary, you use square brackets and you pass in the key name. And so that's what we're doing here. So running this all the way through. I need to come back up here and turn off my breakpoint. You should get output that looks something like this.