WEBVTT 1 00:00:02.240 --> 00:00:05.900 So I continue on with that discussion of dictionaries in Python, 2 00:00:05.900 --> 00:00:08.180 that we've worked on in the last two videos. 3 00:00:08.180 --> 00:00:11.430 Again, just recapping what we did in these last few lines. 4 00:00:11.430 --> 00:00:13.560 We had fruit and it's called keys. 5 00:00:13.560 --> 00:00:15.650 So we got a view object. 6 00:00:15.650 --> 00:00:17.700 When we use the dot keys, that's what we get. 7 00:00:17.700 --> 00:00:20.950 We get a special type, sort of like a list, but it's a view object. 8 00:00:20.950 --> 00:00:23.880 We then printed that out so you can see it on the screen Then we add it 9 00:00:23.880 --> 00:00:27.070 a new item to the dictionary and when we print it out that fruit case again, 10 00:00:27.070 --> 00:00:31.900 even though we haven't directly updated that variable, it was updated by Python. 11 00:00:31.900 --> 00:00:33.320 So it happened behind the scenes. 12 00:00:33.320 --> 00:00:35.960 You can see the tomato was added in the second example. 13 00:00:35.960 --> 00:00:39.190 That again is the only way that view objects can be updated. 14 00:00:39.190 --> 00:00:43.010 Sort of indirectly with the dictionary that's happening behind the scenes. 15 00:00:43.010 --> 00:00:46.360 So we can't add items, and using delete we get an error here. 16 00:00:46.360 --> 00:00:49.520 What we can do is we can convert them to lists. 17 00:00:49.520 --> 00:00:53.600 So we can convert them to lists, and you saw us using the list method. 18 00:00:53.600 --> 00:00:55.140 The list function I should say there list. 19 00:00:55.140 --> 00:00:58.130 And we were quite able to actually convert that to list and 20 00:00:58.130 --> 00:01:00.050 then use the sort function to sort them. 21 00:01:00.050 --> 00:01:04.134 But before we leave our fruit and move on there's one more view we should look at 22 00:01:04.134 --> 00:01:08.474 and that's the items method which returns the dick down got items object which 23 00:01:08.474 --> 00:01:12.615 is another dynamic view object that consists of key and value tuples as well. 24 00:01:12.615 --> 00:01:14.950 So to do that We'll get rid of this code. 25 00:01:16.280 --> 00:01:20.070 Cut that out. Let's close out the run window for now. 26 00:01:21.200 --> 00:01:23.960 And we'll put the print in. 27 00:01:23.960 --> 00:01:24.560 Print fruit. 28 00:01:25.830 --> 00:01:29.114 We can do printfruit.items. 29 00:01:30.910 --> 00:01:32.518 Oops, semicolon there, wrong language. 30 00:01:32.518 --> 00:01:36.270 [LAUGH] Run that, and you can see we've got a different output. 31 00:01:36.270 --> 00:01:42.150 It looks very similar if you recall our videos on tuples. 32 00:01:42.150 --> 00:01:46.110 And as it turns out, we can produce a tuple from it using our tuple function. 33 00:01:46.110 --> 00:01:51.500 So we could do f_tuple equals tuple, they 34 00:01:52.710 --> 00:01:57.507 give us a built in function, fruit.items, 35 00:01:57.507 --> 00:02:02.990 and print f_tuple, like so. 36 00:02:02.990 --> 00:02:08.810 We can run that, and I forgot to put some brackets in there, cuz it's a method call. 37 00:02:08.810 --> 00:02:12.230 Run that And on the last line now you can see we have a regular tuple. 38 00:02:12.230 --> 00:02:16.680 And of course, from that point on we can then use tuple, or 39 00:02:16.680 --> 00:02:21.980 in the case the variable tuple just like we used tuples in any early videos. 40 00:02:21.980 --> 00:02:26.043 For example, we could do something like for snack N 41 00:02:28.273 --> 00:02:34.353 quatupple item common description, 42 00:02:34.353 --> 00:02:43.210 equals snack print item plus is past description. 43 00:02:43.210 --> 00:02:47.360 And we can run that, and you can see we've got our items there. 44 00:02:47.360 --> 00:02:48.860 What was the key from the dictionary, and 45 00:02:48.860 --> 00:02:52.540 also the value from the dictionary because we've created a topple. 46 00:02:52.540 --> 00:02:55.260 We've then got them in our description variables. 47 00:02:55.260 --> 00:02:57.810 Now this wouldn't be Python if it wasn't possible to go the other way. 48 00:02:57.810 --> 00:03:02.890 In other words to create a dictionary from topples of key values pairs, and 49 00:03:02.890 --> 00:03:05.450 the dict constructor allows us to do just that. 50 00:03:05.450 --> 00:03:10.470 So I walk down here On this line we could type print. 51 00:03:10.470 --> 00:03:14.010 Then use the built-in dict function and 52 00:03:14.010 --> 00:03:16.280 pass a tuple to it, the tuple that we've created. 53 00:03:17.440 --> 00:03:24.465 If you run that As you can see that there, that's now just our regular dictionary, 54 00:03:24.465 --> 00:03:27.715 probably not terribly exciting to you because all we've really done is got 55 00:03:27.715 --> 00:03:32.800 the original directory back that we had that we created. 56 00:03:32.800 --> 00:03:33.660 On line one. 57 00:03:33.660 --> 00:03:37.530 But what it does do is demonstrate how a dictionary can be created from a tuple 58 00:03:37.530 --> 00:03:39.180 which is extremely useful. 59 00:03:39.180 --> 00:03:43.280 There is a lot of interaction between dictionaries, tuples and lists. 60 00:03:43.280 --> 00:03:45.130 And later in the course we'll summarize everything we've 61 00:03:45.130 --> 00:03:46.320 done with these data structures. 62 00:03:46.320 --> 00:03:50.210 And what we'll also do is include a list of all the methods that apply to each one. 63 00:03:50.210 --> 00:03:52.280 And now moving onto something that's completely different, 64 00:03:52.280 --> 00:03:53.910 I'll just close down that one window. 65 00:03:53.910 --> 00:03:56.100 Now rather than try to cover everything at once, and 66 00:03:56.100 --> 00:03:59.380 have it all become terribly overwhelming, what we've been doing is we've been 67 00:03:59.380 --> 00:04:03.600 introducing methods and functions as it becomes appropriate to use them. 68 00:04:03.600 --> 00:04:07.830 So now's a good time to introduce another of the string methods Which is joined. 69 00:04:07.830 --> 00:04:11.530 By now you should be used to the idea of mutable and immutable objects. 70 00:04:11.530 --> 00:04:13.440 If you remember strings are immutable. 71 00:04:13.440 --> 00:04:16.610 As a result this means that it's not at all efficient to concatenate 72 00:04:16.610 --> 00:04:18.320 strings in a loop. 73 00:04:18.320 --> 00:04:23.180 Anytime you concatenate a string to an existing string, the new string has to be 74 00:04:23.180 --> 00:04:27.700 created and if that's done in a loop it becomes expensive and slow. 75 00:04:27.700 --> 00:04:29.830 So you really do want to avoid that. 76 00:04:29.830 --> 00:04:32.660 The string join method is there to actually help you. 77 00:04:32.660 --> 00:04:36.540 It actually takes a sequence and produces a string from it. 78 00:04:36.540 --> 00:04:39.420 So the items in the new string are separated by 79 00:04:39.420 --> 00:04:41.270 the stream that join was called upon. 80 00:04:41.270 --> 00:04:43.910 So as said a lot of these things are probably easier to demonstrate rather then 81 00:04:43.910 --> 00:04:45.120 for me to explain it any further. 82 00:04:45.120 --> 00:04:50.110 So I'm gonna create a new file now to leave the [INAUDIBLE] as it was. 83 00:04:50.110 --> 00:04:50.780 New Python file. 84 00:04:50.780 --> 00:04:55.170 And we'll just call this one join for want of a better word. 85 00:04:55.170 --> 00:04:57.401 So let's create a list first. 86 00:04:57.401 --> 00:05:04.263 And myList = ["a","b","c","d"]. 87 00:05:04.263 --> 00:05:07.790 newString = "", just an empty string for now. 88 00:05:08.830 --> 00:05:13.390 And we're going to go through, iterate through my list. 89 00:05:13.390 --> 00:05:18.500 We'll see in my list newString += c + ", " like so, and 90 00:05:18.500 --> 00:05:24.062 then we'll print(newString). 91 00:05:24.062 --> 00:05:26.090 So I consider this first example, and 92 00:05:26.090 --> 00:05:30.240 I will just make sure I'm running this right one, running join. 93 00:05:30.240 --> 00:05:32.650 ABCD so you can see has appeared on the screen. 94 00:05:32.650 --> 00:05:35.760 So as I mentioned this is not a good way of producing code. 95 00:05:35.760 --> 00:05:37.180 So, a couple of problems here, 96 00:05:37.180 --> 00:05:40.280 leaving aside the fact firstly there's a comma on the end of our output. 97 00:05:40.280 --> 00:05:44.450 It's also inefficient because every time through the loop it's creating 98 00:05:44.450 --> 00:05:49.440 a new copy of new string because again the strings that we're using are immutable. 99 00:05:49.440 --> 00:05:53.330 Now augmented assignment will help We did say that talked about that earlier in this 100 00:05:53.330 --> 00:05:57.010 course it does try to update the variable in place where possible but 101 00:05:57.010 --> 00:06:00.090 this is not one of those times where its possible so in other words because strings 102 00:06:00.090 --> 00:06:03.910 are unbeatable augmented a summit isn't gonna help you so what you'd want 103 00:06:03.910 --> 00:06:08.770 to do in this scenario is you want to use join to fix this problem and the way we 104 00:06:08.770 --> 00:06:13.360 would use that Instead of putting plus equals we will delete that out. 105 00:06:13.360 --> 00:06:17.640 We put newString = ", ".join(myList). 106 00:06:17.640 --> 00:06:18.390 And we run that. 107 00:06:19.940 --> 00:06:26.020 Notice we haven't got the trailing comma now. 108 00:06:26.020 --> 00:06:27.680 So is much more efficient. 109 00:06:27.680 --> 00:06:30.420 It also solves the problem of the trailing comme, as you can see there. 110 00:06:30.420 --> 00:06:33.700 And it's really much more Pythonic in that 111 00:06:33.700 --> 00:06:37.890 it's working in a way that Python would intend to work for something like this. 112 00:06:37.890 --> 00:06:41.810 So the way this particular method works is it creates a new string by taking each 113 00:06:41.810 --> 00:06:45.280 item in a sequence which is a list as you can see that are created on line 1. 114 00:06:45.280 --> 00:06:50.400 And separates the item with a string comma that join is actually invoked on. 115 00:06:50.400 --> 00:06:53.350 So we can use any sequence tied by some parameter to join. 116 00:06:53.350 --> 00:06:56.480 So for example instead of a list we can pass another string. 117 00:06:56.480 --> 00:06:58.270 So I'm just gonna place some code in here. 118 00:06:58.270 --> 00:06:59.920 You see what I mean. 119 00:06:59.920 --> 00:07:05.130 So let's change that to letters. 120 00:07:05.130 --> 00:07:07.050 And instead of my list, make that letters. 121 00:07:07.050 --> 00:07:10.555 Of course, you can see See the letters there is now a string instead of a list. 122 00:07:10.555 --> 00:07:14.405 If we run that then we get the same output as you can see. 123 00:07:14.405 --> 00:07:18.375 The same processing, I've since gone from a to Zed because the input was 124 00:07:18.375 --> 00:07:22.325 a string in this case containing the letters a to z. 125 00:07:22.325 --> 00:07:24.935 The other thing to remember is that the string we called joined method on can be 126 00:07:24.935 --> 00:07:26.725 quite complex as well. 127 00:07:26.725 --> 00:07:30.596 If I for example, come back here or close the again. 128 00:07:32.270 --> 00:07:34.800 Numbers, let's try instead of just putting a comma, we'll put 129 00:07:38.994 --> 00:07:43.200 And then .join numbers. 130 00:07:43.200 --> 00:07:47.830 Run that, you see one Mississippi, two Mississippi, three Mississippi. 131 00:07:47.830 --> 00:07:52.730 And so I probably to fix the Spacing, then again you can say 1 Mississippi, 132 00:07:52.730 --> 00:07:55.620 2 Mississippi, 3 Mississippi, and so on. 133 00:07:55.620 --> 00:07:57.070 Again in case you're wondering why, 134 00:07:57.070 --> 00:07:59.680 separating numbers with the word Mississippi is a very rough way to count 135 00:07:59.680 --> 00:08:02.340 seconds, if you don't have a stopwatch handy. 136 00:08:02.340 --> 00:08:04.480 And you could also use 2000 in place of Mississippi. 137 00:08:04.480 --> 00:08:06.030 But anyway I'm getting off track there. 138 00:08:06.030 --> 00:08:08.820 You might have guessed that we're about to concatenate the keys of a dictionary to 139 00:08:08.820 --> 00:08:10.520 give us a stream and you'd be right. 140 00:08:10.520 --> 00:08:13.930 So let's get to doing that because of course this is all about dictionaries. 141 00:08:13.930 --> 00:08:17.260 What we're gonna do is we're going to use dictionaries to store locations in a very 142 00:08:17.260 --> 00:08:18.770 basic adventure game. 143 00:08:18.770 --> 00:08:21.840 We'll also store the available exits in another dictionary and we're gonna link 144 00:08:21.840 --> 00:08:25.710 the two together so that players can move from one location to another. 145 00:08:25.710 --> 00:08:27.790 So I'm just gonna bring a little image up on the screen now so 146 00:08:27.790 --> 00:08:29.550 you can see what I'm talking about. 147 00:08:30.670 --> 00:08:32.850 You can see there that there is five locations a road, 148 00:08:32.850 --> 00:08:35.870 a hill, a building, a valley, and a forest. 149 00:08:35.870 --> 00:08:37.670 The roads are not all straight and you can see this. 150 00:08:37.670 --> 00:08:41.510 That going north from the hill takes you to the forest entering from the west. 151 00:08:41.510 --> 00:08:43.550 So you can go west from the forest to get back to the hill. 152 00:08:43.550 --> 00:08:48.020 What we are going to do What we can do is we can represent the map as a dictionary. 153 00:08:48.020 --> 00:08:51.450 And although we could use the short names of the location as the key, 154 00:08:51.450 --> 00:08:54.730 what we're gonna do is use numbers instead cuz that's easy to read initially. 155 00:08:54.730 --> 00:08:57.290 And if you want to replace one with rho etc., a simple search and 156 00:08:57.290 --> 00:08:59.460 replace will do that once everything's working. 157 00:08:59.460 --> 00:09:03.510 So what I'm going to do now is just copy and paste that code for that dictionary. 158 00:09:04.880 --> 00:09:06.500 I'm just going to replace the code we worked on here. 159 00:09:06.500 --> 00:09:10.710 So I'm gonna paste it in there like so, and you can see we've got the 5 locations 160 00:09:10.710 --> 00:09:14.860 and the keys you can see for each value in the dictionary is the number, 161 00:09:14.860 --> 00:09:17.610 and then it's obviously the description, which is the, and 162 00:09:17.610 --> 00:09:20.710 the other one, the other part is the value as well for the dictionary. 163 00:09:20.710 --> 00:09:22.050 So you've got the key and the value for 164 00:09:22.050 --> 00:09:24.570 each and the 5 locations on the screen as you can see there. 165 00:09:24.570 --> 00:09:27.310 Incidentally, I think that's a lot easier to do when you're adding dictionary 166 00:09:27.310 --> 00:09:30.050 entries like this, creating a dictionary, it is better to separate them. 167 00:09:30.050 --> 00:09:32.590 So I put a comma and separate them on a separate them on a separate line. 168 00:09:32.590 --> 00:09:36.130 It's just easier to read and sort of avoids your dictionaries becoming a little 169 00:09:36.130 --> 00:09:39.370 bit too messy as can happen if you've got them all sort of linked up on a line. 170 00:09:40.450 --> 00:09:42.870 You can see I did it in there as well with the fruit. 171 00:09:42.870 --> 00:09:45.300 So what we now need to do, we've got our basic locations, 172 00:09:45.300 --> 00:09:48.480 we need to represent the available exits from each location. 173 00:09:48.480 --> 00:09:52.400 Comparing to mine again is four plus one exits, north, south, east, and west, 174 00:09:52.400 --> 00:09:54.280 plus quit to exit the game. 175 00:09:54.280 --> 00:09:56.120 So quit's going to available everywhere, 176 00:09:56.120 --> 00:09:57.990 otherwise the available exits will match the map. 177 00:09:57.990 --> 00:10:02.480 So again, looking at the map again What we;re trying to do now is 178 00:10:02.480 --> 00:10:06.810 basically duplicating this set of options which direction is valid. 179 00:10:06.810 --> 00:10:07.500 A set, but 180 00:10:07.500 --> 00:10:12.020 also enabling a quit to actually enable you to quit the game at any point. 181 00:10:12.020 --> 00:10:14.770 From the road for example, we can go in all four directions, but 182 00:10:14.770 --> 00:10:16.650 if we go east to the building. 183 00:10:16.650 --> 00:10:19.470 The only exit from there is gonna be ways back to road. 184 00:10:19.470 --> 00:10:22.020 So one way we can represent this would be a dictionary with 185 00:10:22.020 --> 00:10:23.950 the directions being the key and 186 00:10:23.950 --> 00:10:28.370 the values being the location that we end up at if we go in that direction. 187 00:10:28.370 --> 00:10:31.290 So from one, remember in our key one is the road, 188 00:10:31.290 --> 00:10:35.410 if you go west from there, we'll end up at two, which is the hill. 189 00:10:35.410 --> 00:10:38.790 And if you go east We get to three, the building. 190 00:10:38.790 --> 00:10:42.020 So when we get to a new location, we should print out the description. 191 00:10:42.020 --> 00:10:44.760 And that's gonna be easy to do because each direction 192 00:10:44.760 --> 00:10:48.960 gives us the key of the destination in the locations directory, if that makes sense. 193 00:10:48.960 --> 00:10:50.700 So what I'm gonna do is paste this in now. 194 00:10:50.700 --> 00:10:56.120 Our next dictionary is going to be exits, like so. 195 00:10:56.120 --> 00:10:59.350 And you can see now that we're using a key for the direction And 196 00:10:59.350 --> 00:11:02.880 the value is of a key from the locations. 197 00:11:02.880 --> 00:11:06.400 And again, the reason we're doing that is so that we can ultimately print out 198 00:11:06.400 --> 00:11:09.770 the location when we move, where we've moved to in other words. 199 00:11:09.770 --> 00:11:11.760 So again, bringing the image back up on the screen again. 200 00:11:11.760 --> 00:11:15.700 So if we start off on the road, we get the available exits by looking at index 1. 201 00:11:15.700 --> 00:11:18.190 So I'm just going to just put that over there. 202 00:11:18.190 --> 00:11:22.010 Again the exits is represented as a list of dictionary objects. 203 00:11:22.010 --> 00:11:24.280 This contains one dictionary for each location. 204 00:11:24.280 --> 00:11:27.230 Notice this is a list of dictionary objects. 205 00:11:27.230 --> 00:11:30.840 Important distinction to locations which is just a dictionary. 206 00:11:30.840 --> 00:11:34.410 And we're doing it this way ultimately because we can index the list. 207 00:11:34.410 --> 00:11:36.180 We know that element zero is always going to be the same. 208 00:11:36.180 --> 00:11:36.750 We know that element. 209 00:11:36.750 --> 00:11:39.960 One's always going to be the same and so on and so fourth unlike a dictionary. 210 00:11:39.960 --> 00:11:43.880 And the method we come up here isn't necessarily the best way to represent 211 00:11:43.880 --> 00:11:44.800 the exists. 212 00:11:44.800 --> 00:11:48.770 For example, we could use a dictionary with the key being the location number and 213 00:11:48.770 --> 00:11:51.990 the exists perhaps the List of tuples or another dictionary. 214 00:11:51.990 --> 00:11:56.250 But it's probably easy to start this way, and sort of progress to that ladder. 215 00:11:56.250 --> 00:11:58.940 So again, if we start off on the right, we can get the available width just by 216 00:11:58.940 --> 00:12:01.990 looking at index one in the list to get the dictionary. 217 00:12:01.990 --> 00:12:03.460 So again, we start here. 218 00:12:03.460 --> 00:12:06.300 One, come over here to element one on the list. 219 00:12:06.300 --> 00:12:09.580 And, that's the valid entries, or the valid directions and 220 00:12:09.580 --> 00:12:12.550 also the quick to exit for that particular location. 221 00:12:12.550 --> 00:12:16.800 So, standing on the road, going west would go to two, location two which is the hill. 222 00:12:16.800 --> 00:12:18.160 East would go to three. 223 00:12:18.160 --> 00:12:19.470 Which is a Building. 224 00:12:19.470 --> 00:12:21.250 North will go to five which is the Forest and 225 00:12:21.250 --> 00:12:23.440 south would go t four which is a Valley. 226 00:12:23.440 --> 00:12:26.520 And again, we have the Q coming out to quit out which is going to 227 00:12:26.520 --> 00:12:28.810 basically quit the game. 228 00:12:28.810 --> 00:12:34.000 Similarly from the Hill, we get the dictionary in position two on the list. 229 00:12:34.000 --> 00:12:36.770 You can see position two has been defined there on line 10. 230 00:12:36.770 --> 00:12:39.440 The only way we can go from the hill, we can go north to go to 5, 231 00:12:39.440 --> 00:12:40.930 which of course is the forest. 232 00:12:40.930 --> 00:12:44.320 There's no other exit, other than quit, so we can't get back to the road from 233 00:12:44.320 --> 00:12:45.670 the hill, that's the way we've designed the game. 234 00:12:45.670 --> 00:12:48.940 You can see that by the arrows telling you whether it's multi-directional or 235 00:12:48.940 --> 00:12:50.180 single directional. 236 00:12:50.180 --> 00:12:53.230 So now that we've done that, a simple loop should allow us 237 00:12:53.230 --> 00:12:56.690 to allow the player to keep entering directions until it's [INAUDIBLE]. 238 00:12:56.690 --> 00:12:59.430 And each time around the loop, we'll display descriptions of the local in 239 00:12:59.430 --> 00:13:02.340 the available exits, just to make it look good. 240 00:13:02.340 --> 00:13:03.250 So let's try that. 241 00:13:05.130 --> 00:13:09.562 So I'm gonna type [INAUDIBLE] equals one, 242 00:13:09.562 --> 00:13:15.950 while true, and continue until, basically till Our quitters are selected 243 00:13:15.950 --> 00:13:20.805 available exits equals nothing for now. 244 00:13:20.805 --> 00:13:28.820 For direction in exits location we're starting off at location one. 245 00:13:28.820 --> 00:13:32.090 Again we're starting off starting on the road, number one, 246 00:13:32.090 --> 00:13:33.420 that's why I like lock equals one. 247 00:13:35.370 --> 00:13:40.570 So retrieving the dictionary from our list as you can see there for 248 00:13:40.570 --> 00:13:43.510 that particular direction, we're getting the directions and 249 00:13:43.510 --> 00:13:46.548 available booting up our list then, available exits. 250 00:13:46.548 --> 00:13:50.199 Plus equals direction. 251 00:13:52.620 --> 00:13:59.221 Like so. And then we're going to print locations 252 00:13:59.221 --> 00:14:08.220 and loc is equal to zero. 253 00:14:08.220 --> 00:14:08.720 Oops. 254 00:14:14.050 --> 00:14:18.810 Don't need a bracket there. 255 00:14:18.810 --> 00:14:25.792 Then I'm going to type direction equals input available. 256 00:14:25.792 --> 00:14:31.895 [INAUDIBLE] plus available exits Or upper. 257 00:14:31.895 --> 00:14:35.744 [NOISE] Just going to do an empty print. 258 00:14:35.744 --> 00:14:42.519 [NOISE] If direction in exits. 259 00:14:42.519 --> 00:14:46.610 [NOISE] Fixes in such a way, 260 00:14:49.500 --> 00:14:54.870 and lock equals exists so in other words if it's a valid location and 261 00:14:56.920 --> 00:15:00.890 we're gonna sign that direction. 262 00:15:02.068 --> 00:15:10.280 Else, print, You cannot go in that direction. 263 00:15:13.700 --> 00:15:17.260 The program's gonna keep looping until the player goes to location zero. 264 00:15:17.260 --> 00:15:20.260 At that point the programs gonna break out as you can see there 265 00:15:20.260 --> 00:15:21.910 on line 24 when the game ends. 266 00:15:21.910 --> 00:15:26.230 So when a direction is chosen we check that the k exists in the appropriate 267 00:15:26.230 --> 00:15:27.950 dictionary from our list of exits. 268 00:15:27.950 --> 00:15:29.600 And you can see we were doing that. 269 00:15:29.600 --> 00:15:34.270 If direction in exits location or lock, I should say the lock variable. 270 00:15:34.270 --> 00:15:38.190 And then we use the key to retrieve the next location if it does actually exist. 271 00:15:38.190 --> 00:15:41.770 The exits lock and direction. 272 00:15:41.770 --> 00:15:44.800 Again we are using that as an index into the exits list 273 00:15:44.800 --> 00:15:48.910 to retrieve the dictionary containing the available exits of that location. 274 00:15:48.910 --> 00:15:52.220 So when we get to equals one location, one gives the description, 275 00:15:52.220 --> 00:15:56.200 you are standing at the end of the rope, you cross groups, small brick building and 276 00:15:56.200 --> 00:15:57.010 we can actually run that. 277 00:15:58.550 --> 00:16:01.630 So I can see [INAUDIBLE] standing at the end of a rope before a small 278 00:16:01.630 --> 00:16:02.630 brick building. 279 00:16:02.630 --> 00:16:07.530 It shows us we can go east, south, north, quit or west. 280 00:16:07.530 --> 00:16:08.200 I can just type quit. 281 00:16:08.200 --> 00:16:11.370 You're sitting at a computer, learning [INAUDIBLE] I should say, 282 00:16:11.370 --> 00:16:12.980 then it's exited the game as you can see. 283 00:16:12.980 --> 00:16:17.975 If we run that again This time off we go south we're gonna valley 284 00:16:19.250 --> 00:16:23.598 just to confirm that it's south toward valley, so 285 00:16:23.598 --> 00:16:31.484 we shouldn't have to go back towards north so we shouldn't have to go west. 286 00:16:34.040 --> 00:16:38.240 At the top of a hill we go back to west was the hill again and 287 00:16:38.240 --> 00:16:41.240 we shouldn't now be able to go back to the valley because the way the program's been 288 00:16:41.240 --> 00:16:44.330 designed, we should only be able to go to the forest now which would be north. 289 00:16:46.240 --> 00:16:50.140 North and quit, north, you're at a forest. 290 00:16:52.190 --> 00:16:55.170 But the forest and from there we should be able to go back to the road or 291 00:16:55.170 --> 00:16:58.820 back to the hill so it should either be a south or west. 292 00:16:59.980 --> 00:17:03.100 South or west as you can see there and so on and so forth. 293 00:17:03.100 --> 00:17:03.810 We can quit out of that. 294 00:17:04.910 --> 00:17:07.640 So I'm gonna close that run window, and again, 295 00:17:07.640 --> 00:17:12.228 booting up the list in booting up a list of compatible exits, 296 00:17:12.228 --> 00:17:15.370 we're iterating through the keys of the dictionary to do that. 297 00:17:15.370 --> 00:17:18.430 But wait a minute, didn't we just say earlier in this video 298 00:17:18.430 --> 00:17:21.400 That can cannonading strings in a loop wasn't a good idea. 299 00:17:21.400 --> 00:17:24.250 Without the moment it's probably not a problem because let's face it 300 00:17:24.250 --> 00:17:26.510 there's a maximum five keys in the dictionary. 301 00:17:26.510 --> 00:17:29.540 When this game becomes the most popular multiplayer game on the internet and 302 00:17:29.540 --> 00:17:32.410 we've got thousands of people playing it in real time, that 303 00:17:32.410 --> 00:17:36.100 performance hit will become significant and things will slow probably to a call. 304 00:17:36.100 --> 00:17:40.990 So we can actually replace line 17 through 19 literally with one line. 305 00:17:40.990 --> 00:17:46.470 Let's go and do that, we'll put available x is equals dot join, 306 00:17:46.470 --> 00:17:51.520 exits lock dot 307 00:17:51.520 --> 00:17:56.510 keys which you can see is far more efficient has less lines of code to type. 308 00:17:56.510 --> 00:18:00.593 And are not going to be slowed down by that problem we would have trying to 309 00:18:00.593 --> 00:18:01.186 strings. 310 00:18:01.186 --> 00:18:06.170 And we eventually have a huge, really fast application and popular application. 311 00:18:06.170 --> 00:18:07.680 And you can see it should still work. 312 00:18:07.680 --> 00:18:08.930 We still get the same results. 313 00:18:08.930 --> 00:18:09.759 Here we can go North. 314 00:18:11.410 --> 00:18:16.010 The forest of [INAUDIBLE] he's sitting in front of a computer so that's good. 315 00:18:16.010 --> 00:18:19.230 Of course the other thing we managed to do by doing that approach was we got 316 00:18:19.230 --> 00:18:22.590 rid of the trailing comma, which is good as well. 317 00:18:22.590 --> 00:18:26.930 You might be wondering, we use direction, why didn't we use the word exit. 318 00:18:26.930 --> 00:18:29.960 Probably make more sense to say the x at the exit variable there, 319 00:18:29.960 --> 00:18:30.840 because that's really what it is. 320 00:18:30.840 --> 00:18:33.500 We're trying to find the exit to the current location. 321 00:18:33.500 --> 00:18:36.490 The reason is x is the key within python, quite a dramatic one as well. 322 00:18:36.490 --> 00:18:38.820 Of course, [INAUDIBLE] quit immediately. 323 00:18:38.820 --> 00:18:40.850 So that's why we use direction instead. 324 00:18:40.850 --> 00:18:43.960 And incidentally this is a good example of when you would check for 325 00:18:43.960 --> 00:18:47.310 the existence of a key rather than using get with the default value. 326 00:18:47.310 --> 00:18:50.220 If we'd use get here, you remember we talked about this in the previous video, 327 00:18:50.220 --> 00:18:52.870 and wether we had a specific default value, or 328 00:18:52.870 --> 00:18:55.030 we just relied on it returning none. 329 00:18:55.030 --> 00:18:57.770 Then what we have to do is we still have to check through a ten valley before 330 00:18:57.770 --> 00:19:01.620 we can use the key and direction to retrieve the next location. 331 00:19:01.620 --> 00:19:07.730 In this case, it makes far more sense to just use the key using in before using it. 332 00:19:07.730 --> 00:19:11.000 Remember the course that the players could top anything, not just a letter for 333 00:19:11.000 --> 00:19:12.240 the valuable exits. 334 00:19:12.240 --> 00:19:15.330 Now, the only other comments to make are that we're displaying the available exists 335 00:19:15.330 --> 00:19:16.140 in an input statement. 336 00:19:16.140 --> 00:19:19.450 And we used upper on the input string, so that the player could type in upper or 337 00:19:19.450 --> 00:19:20.540 lowercase. 338 00:19:20.540 --> 00:19:22.020 This is the line here on line 24.upper. 339 00:19:22.020 --> 00:19:25.280 There's a slight problem with this program, though. 340 00:19:25.280 --> 00:19:26.190 Not with the code, but 341 00:19:26.190 --> 00:19:30.490 for us as programmers when we come to add new locations, and we're using a list 342 00:19:30.490 --> 00:19:34.670 to store the and relying on the order of the items in the list. 343 00:19:34.670 --> 00:19:39.410 Now that's fine as far as Python goes, because lists are ordered, When we start 344 00:19:39.410 --> 00:19:43.040 adding 50 or 60 locations and we don't necessarily add them in numerical order, 345 00:19:43.040 --> 00:19:46.640 we're gonna have to camp down the lines of code to make sure new entries 346 00:19:46.640 --> 00:19:48.670 are in the correct position when we build the list. 347 00:19:48.670 --> 00:19:50.740 Now, that will rapidly get tedious. 348 00:19:50.740 --> 00:19:54.180 And as well as that's gonna be a really excellent source of potential errors. 349 00:19:54.180 --> 00:19:57.250 So it would be much better if we use the location's key 350 00:19:57.250 --> 00:19:59.610 Also access its exits dictionary. 351 00:19:59.610 --> 00:20:03.170 Now there's another minor problem in that some players might type west. 352 00:20:03.170 --> 00:20:05.820 They'll put capital W and a lower case E-S-T instead of just W. 353 00:20:05.820 --> 00:20:07.800 In upper or lowercase. 354 00:20:07.800 --> 00:20:10.130 Now that doesn't matter in this case. 355 00:20:10.130 --> 00:20:13.990 But really what we should do is allow them to top the full direction 356 00:20:13.990 --> 00:20:15.720 as well as things like go west. 357 00:20:15.720 --> 00:20:18.440 Now we could achieve this by creating a dictionary containing all 358 00:20:18.440 --> 00:20:21.790 the words players may use as keys with the values perhaps being the word or 359 00:20:21.790 --> 00:20:23.560 letter that we could understand. 360 00:20:23.560 --> 00:20:25.500 Now that seems like a good idea for a challenge, so 361 00:20:25.500 --> 00:20:28.190 we're gonna do that challenge and talk about a challenge to try and 362 00:20:28.190 --> 00:20:29.710 solve that problem in the next video. 363 00:20:29.710 --> 00:20:33.290 And just one other little thing I type exists instead of exits. 364 00:20:33.290 --> 00:20:34.500 I fixed that up. 365 00:20:34.500 --> 00:20:38.400 And probably what we should do here also is we should add a space like so. 366 00:20:38.400 --> 00:20:43.020 And that's so when we run this, there's a space there between 367 00:20:43.020 --> 00:20:46.210 the end of the Available exits, and where the user can actually start typing. 368 00:20:46.210 --> 00:20:48.020 So anyway, I'm gonna end this video here now. 369 00:20:48.020 --> 00:20:50.860 In the next video we're gonna tackle those issues I've talked about 370 00:20:50.860 --> 00:20:51.900 in the form of a challenge. 371 00:20:51.900 --> 00:20:53.140 So I hope to see you in that next video.