WEBVTT 1 00:00:02.240 --> 00:00:04.620 Okay. So it's time for a Python challenge. 2 00:00:04.620 --> 00:00:08.260 We're actually going to do two challenges for these, for 3 00:00:08.260 --> 00:00:10.160 the dictionary sort of things. 4 00:00:10.160 --> 00:00:13.860 So the first one, let me introduce, I'm gonna copy this code because 5 00:00:13.860 --> 00:00:17.540 ultimately we're going to be using that and I'm gonna create a new file. 6 00:00:17.540 --> 00:00:19.350 A new Python File. 7 00:00:19.350 --> 00:00:20.360 And we'll call this challenge. 8 00:00:22.750 --> 00:00:23.830 I'm just gonna paste it in there. 9 00:00:23.830 --> 00:00:27.960 That's the standard code that we worked on in the previous video. 10 00:00:27.960 --> 00:00:30.320 But what we're going to do, come up to the top here. 11 00:00:32.730 --> 00:00:36.540 We're gonna paste in First challenge, bearing in mind there's a couple of 12 00:00:36.540 --> 00:00:38.670 challenges here, and we saw a couple of sections. 13 00:00:38.670 --> 00:00:43.560 So the challenge at the moment is to modify the program that you see below, so 14 00:00:43.560 --> 00:00:47.210 that the exits are a dictionary rather than a list. 15 00:00:47.210 --> 00:00:50.180 And the keys being the numbers of the locations and the values being 16 00:00:50.180 --> 00:00:53.680 dictionaries holding the exits, and obviously they're doing that at present. 17 00:00:53.680 --> 00:00:57.080 So no change should be needed to the actual code itself, 18 00:00:57.080 --> 00:01:00.810 it's just basically the dictionaries and lists that you're changing. 19 00:01:00.810 --> 00:01:01.820 And once that's working, 20 00:01:01.820 --> 00:01:06.420 we're gonna create another dictionary that contains words That players can use. 21 00:01:06.420 --> 00:01:08.030 Instructions, in other words. 22 00:01:08.030 --> 00:01:10.130 And these words will be the keys, and 23 00:01:10.130 --> 00:01:14.813 the valleys will be a single letter that the can use to determine which way to go. 24 00:01:14.813 --> 00:01:16.600 So that's the challenge. 25 00:01:16.600 --> 00:01:17.760 Two components of a challenge. 26 00:01:17.760 --> 00:01:21.400 And bearing in mind it's also a secondary challenge which you will be doing later. 27 00:01:21.400 --> 00:01:24.080 So go away and see if you can complete both of those and 28 00:01:24.080 --> 00:01:28.740 once you're ready come back and we'll work through both parts of this challenge. 29 00:01:28.740 --> 00:01:29.440 Pause the video now. 30 00:01:32.430 --> 00:01:33.370 Okay, so how did you get on? 31 00:01:33.370 --> 00:01:36.720 Hopefully you managed to resolve that and you've got it working. 32 00:01:36.720 --> 00:01:40.180 So the first stage is actually quite easy so what we really want to do here is just 33 00:01:40.180 --> 00:01:45.840 change exits From a dictionary or sorry from a list I should say to a dictionary. 34 00:01:45.840 --> 00:01:47.200 So let's go ahead now and make the change. 35 00:01:47.200 --> 00:01:47.740 Want to go ahead and 36 00:01:47.740 --> 00:01:51.490 convert the existing list which is on line 18 into a dictionary. 37 00:01:51.490 --> 00:01:55.200 So to do that we're going to start by removing the square brackets which 38 00:01:55.200 --> 00:01:56.300 indicate that it's a list. 39 00:01:59.300 --> 00:02:01.390 Like so and I'll just put a bit of space here so it's a bit clearer. 40 00:02:02.480 --> 00:02:06.696 That we also need to put our keys in for each part so zero colon, 41 00:02:06.696 --> 00:02:10.849 one colon, 42 00:02:10.849 --> 00:02:15.055 two colon. 43 00:02:15.055 --> 00:02:21.745 Three column, four column, and five column. 44 00:02:21.745 --> 00:02:24.295 So the keys are obviously the rooms. 45 00:02:24.295 --> 00:02:25.715 The locations as they are before. 46 00:02:25.715 --> 00:02:28.845 And I'll just clear that up a little bit like so. 47 00:02:28.845 --> 00:02:30.625 And that should be all we need to do. 48 00:02:30.625 --> 00:02:33.205 We've converted it now from a list to a dictionary. 49 00:02:33.205 --> 00:02:36.092 If a key being the location 0, 1, 2, 3, 4, 50 00:02:36.092 --> 00:02:40.600 5 Which matches our location dictionary above, so if we run that, and 51 00:02:43.460 --> 00:02:47.200 we should still be able to go say north you're in the forest, 52 00:02:47.200 --> 00:02:52.700 south you're standing at the end of a road we can quit, he's sitting 53 00:02:52.700 --> 00:02:55.450 in front of a computer learning Python so that's the first part of the challenge. 54 00:02:56.560 --> 00:02:58.150 Converting it to a dictionary and 55 00:02:58.150 --> 00:03:01.520 note how we didn't have to change any of the code that's the beauty 56 00:03:01.520 --> 00:03:05.980 that if we're using a numerical key for a dictionary, the syntax for retrieving 57 00:03:05.980 --> 00:03:10.400 the dictionary value by key is the same as retrieving a list item by index. 58 00:03:10.400 --> 00:03:13.280 That's one of the great things that happened here, the code doesn't have to 59 00:03:13.280 --> 00:03:17.870 change to accommodate The change in the data structure from list to dictionary. 60 00:03:17.870 --> 00:03:19.430 Okay so for the second part so 61 00:03:19.430 --> 00:03:23.530 we need to create a dictionary whose keys are the words we're interested and 62 00:03:23.530 --> 00:03:26.680 the values of the single letter that the program expects. 63 00:03:26.680 --> 00:03:30.410 So if the pro players talks more than a single letter. 64 00:03:30.410 --> 00:03:32.630 Then we use the vocabulary dictionary. 65 00:03:32.630 --> 00:03:35.420 Because they could type something like run less quickly, 66 00:03:35.420 --> 00:03:38.430 we don't use the text as the value to look up, but we do it the other way. 67 00:03:38.430 --> 00:03:40.870 We iterate through the keys to see if their text 68 00:03:40.870 --> 00:03:43.360 contains one of the words that we recognize. 69 00:03:43.360 --> 00:03:47.540 So, what we need to do first is create our vocabulary, we're going to put it down 70 00:03:47.540 --> 00:03:50.130 here and that's going to be a dictionary with the relevant commands. 71 00:03:51.320 --> 00:03:58.120 So, we're going to start by typing vocabulary equals and 72 00:03:58.120 --> 00:04:02.673 the first one will be quit and the key, the value will be two. 73 00:04:02.673 --> 00:04:11.816 North, north, south, 74 00:04:16.537 --> 00:04:19.815 east, and west. 75 00:04:19.815 --> 00:04:24.720 [SOUND] And we can just tidy it up a little bit so 76 00:04:24.720 --> 00:04:26.720 it sits there nicely, but that's optional. 77 00:04:26.720 --> 00:04:29.120 So we've now assigned the vocabulary. 78 00:04:29.120 --> 00:04:29.900 The dictionary. 79 00:04:29.900 --> 00:04:32.660 We've assigned and created a dictionary and assigned it to vocabulary. 80 00:04:32.660 --> 00:04:33.650 The variable. 81 00:04:33.650 --> 00:04:35.780 So what we want to do now is add some codes. 82 00:04:35.780 --> 00:04:38.750 So once we've actually got a command that's been given to us and 83 00:04:38.750 --> 00:04:40.210 the users selected that and typed it in. 84 00:04:40.210 --> 00:04:42.530 Down here we want to start adding some code. 85 00:04:42.530 --> 00:04:47.559 So we want to parse the input so we're gonna parse user 86 00:04:47.559 --> 00:04:55.980 report using our vocabulary dictionary if necessary. 87 00:04:57.620 --> 00:05:00.260 And we're only going to do that remember if the length is greater than one. 88 00:05:00.260 --> 00:05:07.240 So I'm going to put if, len, direction, direction, greater than one. 89 00:05:09.300 --> 00:05:14.370 In other words, more than one letter. 90 00:05:14.370 --> 00:05:16.430 Check out vocab. 91 00:05:16.430 --> 00:05:19.940 Then we put four word in vocabulary. 92 00:05:21.770 --> 00:05:26.790 So what effectively we're doing now is does it contain a word we know? 93 00:05:29.420 --> 00:05:35.580 If word in direction, if it does then direction, 94 00:05:35.580 --> 00:05:40.464 direction becomes equal to vocabulary(word) and 95 00:05:40.464 --> 00:05:42.620 we'll just clean it up a little bit. 96 00:05:42.620 --> 00:05:45.280 And that's actually it as you can see then. 97 00:05:45.280 --> 00:05:47.660 Remember, we're also converting it to upper case as well. 98 00:05:47.660 --> 00:05:50.100 So we're really only testing to see whatever they have 99 00:05:50.100 --> 00:05:52.910 typed is greater than 1, so more than one character. 100 00:05:52.910 --> 00:05:57.160 If it is, we're gonna go through our dictionary and try to find a match for 101 00:05:57.160 --> 00:05:57.980 whatever they have typed. 102 00:05:57.980 --> 00:06:00.080 And if they have typed something like north, 103 00:06:00.080 --> 00:06:03.030 we're gonna match it back to the value N as you can see there. 104 00:06:03.030 --> 00:06:04.750 Let's actually run this and see if it works. 105 00:06:04.750 --> 00:06:06.970 So, first thing, you should just check. 106 00:06:06.970 --> 00:06:09.160 If you go north, you're in a forest. 107 00:06:09.160 --> 00:06:11.080 South, you're standing in the road. 108 00:06:11.080 --> 00:06:15.120 And if you type north, you're in a forest. 109 00:06:16.980 --> 00:06:19.340 But also you should be able to type, go north. 110 00:06:20.990 --> 00:06:25.540 Your gonna first go south, and the reason is 111 00:06:25.540 --> 00:06:29.520 we're actually done looking at our and we're going through our dictionary and 112 00:06:29.520 --> 00:06:32.510 we're comparing each word of the dictionary to see whether it's fine or 113 00:06:32.510 --> 00:06:37.500 whatevers being So, the entire string is going south We are checking to see if 114 00:06:37.500 --> 00:06:40.420 the word South is actually in that string and that's why it's working that way 115 00:06:40.420 --> 00:06:45.210 even though we've typed extra letters that are not in it, exact match so to speak. 116 00:06:45.210 --> 00:06:47.380 So what we now got is a fairly flexible way for 117 00:06:47.380 --> 00:06:50.840 players to specify which direction they'd like to go in. 118 00:06:50.840 --> 00:06:53.930 And for that reason alone hopefully that's demonstrated the power of using 119 00:06:53.930 --> 00:06:54.640 dictionaries. 120 00:06:54.640 --> 00:06:58.860 Once you are actually used to using dictionaries Even our exits one. 121 00:06:58.860 --> 00:07:02.940 The exits diction where you will be converted makes a lot more sense, but 122 00:07:02.940 --> 00:07:06.130 you can probably see why we chose to use a list initially and 123 00:07:06.130 --> 00:07:08.640 that's because a dictionary of dictionaries 124 00:07:08.640 --> 00:07:10.845 can look confusing at first until you sort of understand the. 125 00:07:10.845 --> 00:07:11.475 Concepts. 126 00:07:11.475 --> 00:07:14.305 Now with that said, we can make the program a bit more efficient though. 127 00:07:14.305 --> 00:07:17.925 So it's quite likely that our vocabulary will grow quite large, 128 00:07:17.925 --> 00:07:21.015 whereas the text the players type in will only be a few words. 129 00:07:21.015 --> 00:07:21.675 So as a result, 130 00:07:21.675 --> 00:07:25.310 it's really not efficient to perform the word lookup the way we have. 131 00:07:25.310 --> 00:07:29.720 As we could literally end up iterating through a large collection of keys before 132 00:07:29.720 --> 00:07:32.550 we find the one that's in the player's input, 133 00:07:32.550 --> 00:07:34.140 that's contained in the player's input. 134 00:07:34.140 --> 00:07:38.060 A better way would be to check each word that the player has typed to 135 00:07:38.060 --> 00:07:39.900 see if it's in the vocabulary. 136 00:07:39.900 --> 00:07:43.540 Cuz obviously, starting from that way, rather than coming from the. 137 00:07:43.540 --> 00:07:45.770 Starting at the vocabulary and going through that. 138 00:07:45.770 --> 00:07:46.600 Better to go the other way. 139 00:07:46.600 --> 00:07:49.150 And Python provides a way to do just that. 140 00:07:49.150 --> 00:07:52.010 So what we need to do is break up the input that the [INAUDIBLE]. 141 00:07:52.010 --> 00:07:56.260 Applies topped into individual words in other words individual sequences of 142 00:07:56.260 --> 00:08:00.640 characters separated by spaces so really what we want to do there is we want to do 143 00:08:00.640 --> 00:08:04.850 the opposite of join and in fact there is a method to do that and its called split. 144 00:08:04.850 --> 00:08:06.550 And it does exactly what we want. 145 00:08:06.550 --> 00:08:11.290 So given a string, the split command or the split 146 00:08:11.290 --> 00:08:15.880 method will split a string into a list containing all the parts of the string 147 00:08:15.880 --> 00:08:20.320 that are separated by the delimiter of our choice, which is gonna be a space here. 148 00:08:20.320 --> 00:08:24.010 And if we don't specify a delimiter, it defaults to a space. 149 00:08:24.010 --> 00:08:24.920 But we could use commas, 150 00:08:24.920 --> 00:08:28.540 colons or any other type of text that we want as a delimiter. 151 00:08:28.540 --> 00:08:30.560 So let's have a look at, see that in action. 152 00:08:30.560 --> 00:08:33.860 What we want to do before we start, we want to comment 153 00:08:33.860 --> 00:08:36.360 out the call of the [INAUDIBLE] So I'm going to close this down. 154 00:08:37.500 --> 00:08:38.820 Disconnect. 155 00:08:38.820 --> 00:08:42.090 Let's go ahead and comment all that code out for now, cuz we don't need it. 156 00:08:42.090 --> 00:08:43.560 Come back to that later. 157 00:08:43.560 --> 00:08:47.350 Comment all that out again with command slash, which is control slash for 158 00:08:47.350 --> 00:08:53.070 Windows and Linux after selecting it, and we'll put our code up here We're probably 159 00:08:53.070 --> 00:08:56.676 coming to that area as well, the lock = 1 because that's all part and parcel of it. 160 00:08:56.676 --> 00:08:59.570 So let's actually try the split function, let's do a couple of examples. 161 00:08:59.570 --> 00:09:03.920 We can do a print locations 0. 162 00:09:03.920 --> 00:09:12.540 split, and also try print locations 3. 163 00:09:12.540 --> 00:09:13.360 split. 164 00:09:15.100 --> 00:09:16.380 Passing a parameter this time. 165 00:09:16.380 --> 00:09:17.590 I was, or, comma. 166 00:09:19.490 --> 00:09:20.740 And let's also try this one. 167 00:09:20.740 --> 00:09:22.031 Print. Space. 168 00:09:22.031 --> 00:09:22.559 Dot. 169 00:09:22.559 --> 00:09:24.842 Join locations. 170 00:09:24.842 --> 00:09:25.545 Zero. 171 00:09:25.545 --> 00:09:26.073 Dot. 172 00:09:26.073 --> 00:09:27.127 Spirit. 173 00:09:27.127 --> 00:09:33.220 [SOUND] So, let's run this. 174 00:09:33.220 --> 00:09:35.970 So you can see what happened with the first one. 175 00:09:35.970 --> 00:09:39.750 It splits, you are sitting in front of a computer learning Python, into a list 176 00:09:39.750 --> 00:09:43.350 containing the 10 words, which is pretty cool that it's done that automatically. 177 00:09:43.350 --> 00:09:47.940 The 2nd example gave a list, gives a list containing 2 items, as you can see, 178 00:09:47.940 --> 00:09:52.650 because it's split based on the fact that we passed the parameter of a comma, so 179 00:09:52.650 --> 00:09:56.860 it's using that a as a delimiter instead of the default being a space. 180 00:09:56.860 --> 00:09:59.810 Which the split method will use unless you give it a parameter. 181 00:09:59.810 --> 00:10:02.800 And incidentally if you didn't want a leading space in the second list item, 182 00:10:02.800 --> 00:10:06.840 you could specify comma as a string to split on if you wanted to do it that way. 183 00:10:06.840 --> 00:10:08.710 So split's the opposite of join so 184 00:10:08.710 --> 00:10:12.910 the final print is interesting if as it turns out a little futile. 185 00:10:12.910 --> 00:10:18.260 So it uses split to split the text that spaces, but then it also calls join 186 00:10:18.260 --> 00:10:21.190 on a string containing a single space to join them back up again. 187 00:10:21.190 --> 00:10:24.170 So really the end result is the orignal string that you worked with. 188 00:10:24.170 --> 00:10:26.240 Ok, so armed with this split method, 189 00:10:26.240 --> 00:10:29.640 we can rewrite the program to work the correct way around. 190 00:10:29.640 --> 00:10:32.660 So I can check the players input against the vocabulary, 191 00:10:32.660 --> 00:10:33.970 instead of the other way around. 192 00:10:33.970 --> 00:10:35.360 So let's go back and do that. 193 00:10:35.360 --> 00:10:40.690 And I'm going to comment these [INAUDIBLE] out, like so, and I'm going to select 194 00:10:40.690 --> 00:10:44.975 the [INAUDIBLE] code that we Comment it out, and do a command slash again. 195 00:10:44.975 --> 00:10:47.877 Just control slash on Windows and Linux, and that makes it, 196 00:10:47.877 --> 00:10:51.335 more [INAUDIBLE] gets rid of the comments, so that we can work on it again. 197 00:10:51.335 --> 00:10:53.205 So what we want to do here is see, 198 00:10:53.205 --> 00:10:56.765 in this method is this part here after the [INAUDIBLE] direction. 199 00:10:56.765 --> 00:10:58.898 You really just want to comment that out for now. 200 00:10:58.898 --> 00:11:00.800 [INAUDIBLE] I'll do is I'll delete that. 201 00:11:00.800 --> 00:11:04.300 So after the test to make sure that what's been taught has more than one characters 202 00:11:04.300 --> 00:11:09.760 in it, we do words equals direction of split. 203 00:11:09.760 --> 00:11:14.260 So we're splitting up the input using the default delimiter of a space. 204 00:11:14.260 --> 00:11:17.300 Cuz we're gonna go through those words, so for word in words. 205 00:11:18.400 --> 00:11:22.600 And we're going a test the other way this time and we're putting if word In 206 00:11:23.960 --> 00:11:29.550 vocabulary direction equals vocabulary word. 207 00:11:29.550 --> 00:11:32.080 And we can also break at that point. 208 00:11:33.380 --> 00:11:36.350 So you can see how we've done it the other way now. 209 00:11:36.350 --> 00:11:40.660 So what we've done is we've taken doing our searching based on what the users 210 00:11:40.660 --> 00:11:45.280 typed instead of going through the whole dictionary, which Isn't a problem now, but 211 00:11:45.280 --> 00:11:48.380 if we started having hundreds of entries, it could start slowing it down. 212 00:11:48.380 --> 00:11:52.430 So it's always something that you should be looking at when you're developing code, 213 00:11:52.430 --> 00:11:54.960 to decide which angle should I start from. 214 00:11:54.960 --> 00:11:57.670 Is it best to start, in this case, from the user's input, 215 00:11:57.670 --> 00:12:00.120 because chances are there's only gonna be a few words there, 216 00:12:00.120 --> 00:12:03.880 instead of automatically processing the entire dictionary each time, 217 00:12:03.880 --> 00:12:05.950 which we did in sort of the first version of this code. 218 00:12:05.950 --> 00:12:07.489 Let's run this and make sure it works. 219 00:12:08.900 --> 00:12:12.110 So if we type east, okay, and 220 00:12:12.110 --> 00:12:16.620 now back to west again, and if we type E, go back to west again. 221 00:12:18.800 --> 00:12:20.000 Please sir, can I go east. 222 00:12:22.114 --> 00:12:25.820 And it's gonna say it's found east, so it's gonna [INAUDIBLE] 223 00:12:25.820 --> 00:12:29.580 each one of those words and found east as the last word, And that works. 224 00:12:29.580 --> 00:12:30.490 So we go back west again. 225 00:12:30.490 --> 00:12:33.590 And just to make sure, 226 00:12:33.590 --> 00:12:39.020 how bout going east now again. 227 00:12:39.020 --> 00:12:39.990 And still found that as well. 228 00:12:41.000 --> 00:12:41.820 So that's it. I hope 229 00:12:41.820 --> 00:12:43.110 you got a lot out of that challenge. 230 00:12:43.110 --> 00:12:45.170 So we're not done yet with dictionaries. 231 00:12:45.170 --> 00:12:48.530 In the next video, we're gonna go ahead and continue our work on dictionaries. 232 00:12:48.530 --> 00:12:51.120 And specifically we're going to be looking at the update method 233 00:12:51.120 --> 00:12:53.380 which allows you to combine two dictionaries. 234 00:12:53.380 --> 00:12:57.230 And also a copy method that's used to create a copy of a dictionary and 235 00:12:57.230 --> 00:12:59.930 then we'll go into another challenge for that as well. 236 00:12:59.930 --> 00:13:01.080 So see you in the next video.