WEBVTT 1 00:00:02.340 --> 00:00:04.230 Okay, so it's time for a challenge. 2 00:00:04.230 --> 00:00:07.060 This is the second challenge with dictionaries. 3 00:00:07.060 --> 00:00:09.450 Now we've mentioned that the data for 4 00:00:09.450 --> 00:00:13.050 this adventure game could be organized in many different ways. 5 00:00:13.050 --> 00:00:16.240 And we've created another way for you here in this code. 6 00:00:16.240 --> 00:00:18.860 So your mission, if you chose to accept it. 7 00:00:18.860 --> 00:00:21.180 Is to change the code below to make it work. 8 00:00:21.180 --> 00:00:25.950 Now, the code that is below is the complete program from the last video, 9 00:00:25.950 --> 00:00:30.410 the adventure game but with the location's dictionary modified so 10 00:00:30.410 --> 00:00:33.990 that everything is in a single dictionary. 11 00:00:33.990 --> 00:00:37.210 So what your job here if the challenge is to fix it ANd we'll go down and 12 00:00:37.210 --> 00:00:40.100 have a look at the code, and you'll notice there are some errors here. 13 00:00:40.100 --> 00:00:42.110 There's errors there that you'll need to fix. 14 00:00:42.110 --> 00:00:46.330 So you'll need to get this working because we've not got our descriptions for 15 00:00:46.330 --> 00:00:48.990 the various locations, nox, x, etc. 16 00:00:48.990 --> 00:00:51.700 All in the one sort of master dictionary. 17 00:00:51.700 --> 00:00:52.350 So that's your goal. 18 00:00:52.350 --> 00:00:55.480 Now the thing to remember with this challenge is there's not a lot of code you 19 00:00:55.480 --> 00:00:56.550 need to change. 20 00:00:56.550 --> 00:01:00.870 In fact you should only have to make changes to just four lines of code to 21 00:01:00.870 --> 00:01:01.770 get this to work. 22 00:01:01.770 --> 00:01:04.980 Now check any of your warnings carefully if you're using IntelliJ here. 23 00:01:04.980 --> 00:01:08.610 And don't worry if all four changes give you the same warning. 24 00:01:08.610 --> 00:01:11.190 We will explain why, in the solution. 25 00:01:11.190 --> 00:01:13.620 I'm gonna ask you to pause the video now, go away and 26 00:01:13.620 --> 00:01:15.300 see if you can solve this challenge. 27 00:01:15.300 --> 00:01:18.160 Once you're ready to come back and see the solution, click play and 28 00:01:18.160 --> 00:01:19.340 I'll be here showing you how to do it. 29 00:01:22.525 --> 00:01:25.735 Okay so, hopefully you managed to solve this. 30 00:01:25.735 --> 00:01:26.545 So, let's go through and 31 00:01:26.545 --> 00:01:31.245 show you our solution, and how we would go about fixing this. 32 00:01:31.245 --> 00:01:35.415 So the first thing we need to change here, the error is showing on line 43. 33 00:01:35.415 --> 00:01:40.855 So, clearly the problem there is it's trying to use the exits dictionary, 34 00:01:40.855 --> 00:01:42.725 which no longer exists. 35 00:01:42.725 --> 00:01:44.540 So, we need to change the code. 36 00:01:44.540 --> 00:01:46.560 To use our master dictionary. 37 00:01:46.560 --> 00:01:49.770 So what need to do there is we still need to join it, so availableExits, 38 00:01:49.770 --> 00:01:54.150 that first part is correct but the join now is not exits rather it's on locations. 39 00:01:54.150 --> 00:02:00.240 So we type locations[loc] but then We need to put 40 00:02:00.240 --> 00:02:04.930 exits to use the exits component of the dictionary, and dot keys. 41 00:02:04.930 --> 00:02:09.020 So that's the first change, pretty simple change as you can see. 42 00:02:09.020 --> 00:02:13.150 So we're looking up the key exits and grabbing the keys for those. 43 00:02:13.150 --> 00:02:16.700 Which is now, of course, all enclosed in one master dictionary. 44 00:02:16.700 --> 00:02:19.230 Of which we've got subdictionaries built in. 45 00:02:19.230 --> 00:02:22.220 So we've still got a room location, so you can see there, 46 00:02:22.220 --> 00:02:26.780 the number equated to each room, but we've now got two entries. 47 00:02:26.780 --> 00:02:28.590 Before we had a single dictionary. 48 00:02:28.590 --> 00:02:32.880 We've got D E S C, which is the description for the location and 49 00:02:32.880 --> 00:02:38.170 also got exits Which is our valid exit in the named exits. 50 00:02:38.170 --> 00:02:42.170 So we've separated the three variables that we had, three separate dictionaries 51 00:02:42.170 --> 00:02:44.930 and we put those dictionaries into this one master dictionary. 52 00:02:44.930 --> 00:02:46.030 So each location, 53 00:02:46.030 --> 00:02:49.730 in essence, has now We got three dictionaries as you can see there. 54 00:02:49.730 --> 00:02:50.810 So it's a little bit confusing. 55 00:02:50.810 --> 00:02:56.040 So we got one master as you can see there which contains the description, the exits 56 00:02:56.040 --> 00:03:00.980 and the main exits and of course exits and main exits are themselves dictionaries. 57 00:03:00.980 --> 00:03:05.470 So there's three dictionaries there in total as we can see effectively for 58 00:03:05.470 --> 00:03:06.040 each location. 59 00:03:06.040 --> 00:03:07.700 So a little bit confusing. 60 00:03:07.700 --> 00:03:09.820 But that's the gist of it anyway. 61 00:03:09.820 --> 00:03:13.480 OK, so that was the first fix, so now what we're doing is we're correctly 62 00:03:13.480 --> 00:03:16.450 looking up lock, which is finding the relevant location number, 63 00:03:16.450 --> 00:03:19.940 then we're looking up the exits, so we're using that lookup there to find that 64 00:03:19.940 --> 00:03:25.120 particular dictionary, then using the dot keys to get all the keys that exist for 65 00:03:25.120 --> 00:03:28.320 that particular entry This case which is exits. 66 00:03:28.320 --> 00:03:31.540 So that's the first fix, so I'll just come back here again, 67 00:03:31.540 --> 00:03:36.200 it's got another problem there, print locations, loc isn't going to work. 68 00:03:36.200 --> 00:03:39.520 What we need to do is also use the descriptions. 69 00:03:39.520 --> 00:03:43.130 We need to look that up to make sure we're grabbing it from the right spot. 70 00:03:43.130 --> 00:03:47.370 So I'd need to use that key to retrieve it, so 71 00:03:47.370 --> 00:03:52.750 to do that, we just put, as you can see. 72 00:03:52.750 --> 00:03:57.190 And then that will now correctly find us the description for the location. 73 00:03:58.460 --> 00:04:02.010 So that's two fixes, now these errors here on line 50 and 74 00:04:02.010 --> 00:04:05.910 51 also have to be fixed, and in the first one you'll probably guess that because 75 00:04:05.910 --> 00:04:10.950 it's very similar to what we did on line 43, so we're no longer using the Exits 76 00:04:10.950 --> 00:04:15.310 dictionary as a separate variable, it's now part of our master dictionary So 77 00:04:15.310 --> 00:04:17.890 to fix that, we'll change that to locations. 78 00:04:19.040 --> 00:04:22.260 Loc and then it's going to be exits. 79 00:04:24.700 --> 00:04:25.320 Like so. And 80 00:04:25.320 --> 00:04:28.720 that's now grabbing the exits dictionary and then taking a copy of that. 81 00:04:28.720 --> 00:04:31.870 And likewise, on the next line, you can probably get that with named exits. 82 00:04:31.870 --> 00:04:34.300 We no longer have a separate variable for that. 83 00:04:34.300 --> 00:04:36.090 So we need to fix that as well. 84 00:04:36.090 --> 00:04:39.210 So we're going to leave name exits in because we're gonna use that part of it. 85 00:04:39.210 --> 00:04:45.810 But we're going to make it locations lock, and 86 00:04:46.810 --> 00:04:52.691 then it's going to be, named exits, like so. 87 00:04:54.090 --> 00:04:55.950 We can get rid of the rest of that line now. 88 00:04:55.950 --> 00:04:58.840 And of course I made a typo there so that shouldn't be in there. 89 00:04:58.840 --> 00:04:59.750 Let's fix that up. 90 00:04:59.750 --> 00:05:01.010 So that should now be it. 91 00:05:01.010 --> 00:05:03.710 That should be all we need to run this. 92 00:05:03.710 --> 00:05:05.454 So let's just run it and make sure it works. 93 00:05:10.865 --> 00:05:14.385 We correctly not getting our numbers for the available exits so that's good, 94 00:05:14.385 --> 00:05:15.495 that part's working. 95 00:05:15.495 --> 00:05:20.935 Then we can type go west, you're at the top of a hill, we can go type in road. 96 00:05:20.935 --> 00:05:23.885 Can't go in that direction, which is correct because of where we are, 97 00:05:23.885 --> 00:05:28.300 we can go north You're in the forest, control, type in road again. 98 00:05:28.300 --> 00:05:31.620 You are standing at the end of a road before a small brick building. 99 00:05:31.620 --> 00:05:32.860 We should be able to enter hill. 100 00:05:32.860 --> 00:05:34.910 You are at the top of a hill. 101 00:05:34.910 --> 00:05:37.350 And you can see the available exits are working. 102 00:05:37.350 --> 00:05:38.440 Quit out of that. 103 00:05:38.440 --> 00:05:40.160 You are sitting at a computer learning python. 104 00:05:40.160 --> 00:05:41.610 I'm going to close this down again. 105 00:05:43.350 --> 00:05:44.420 And we could also put in here, 106 00:05:44.420 --> 00:05:47.660 if we wanted to, plus just a space there if we wanted to. 107 00:05:47.660 --> 00:05:48.530 So let me run it. 108 00:05:49.620 --> 00:05:51.710 We've now got that extra space there. 109 00:05:51.710 --> 00:05:53.420 So that's the challenge. 110 00:05:53.420 --> 00:05:54.520 Clean up the errors now. 111 00:05:54.520 --> 00:05:57.980 The lint checker in IntelliJ is complaining, and PyCharm will do the same. 112 00:05:57.980 --> 00:05:58.880 It's gonna show a warning. 113 00:05:58.880 --> 00:06:04.240 So if we come over here and select them, expected type int matching generic types. 114 00:06:04.240 --> 00:06:06.460 And we'll just click on that to see which line it's talking about. 115 00:06:06.460 --> 00:06:08.290 You can see it's line 43. 116 00:06:08.290 --> 00:06:12.670 I should put an arrow fix in there to fix it up, like so. 117 00:06:12.670 --> 00:06:15.660 And the other arrows relate to the two other lines that we actually worked on. 118 00:06:15.660 --> 00:06:19.310 And you can see it's having the same problems as you can see there. 119 00:06:19.310 --> 00:06:23.640 So these are the four lines that we've modified that Intelliga is 120 00:06:23.640 --> 00:06:27.470 saying coming up with a problem, potentially a problem showing a warning. 121 00:06:27.470 --> 00:06:31.740 So the problem is it's complaining that it expected an int, but got a string instead. 122 00:06:31.740 --> 00:06:35.870 So generally speaking, the lint checker, and that's what this process is, 123 00:06:35.870 --> 00:06:38.750 does a really good job of checking your code as you type it. 124 00:06:38.750 --> 00:06:40.550 But that said, it's not perfect. 125 00:06:40.550 --> 00:06:44.960 So the hint does say to press control F1, command F1 on a Mac. 126 00:06:44.960 --> 00:06:48.700 And we get the full message, and basically due to dynamic dispatch and 127 00:06:48.700 --> 00:06:53.330 duck typing, This is possible in a limited, but useful, number of cases. 128 00:06:53.330 --> 00:06:55.600 Now [LAUGH] that's a bit of a cryptic message and 129 00:06:55.600 --> 00:06:57.210 I'll just bring that up on the screen again. 130 00:06:57.210 --> 00:07:01.260 Again, due to dynamic dispatch and duck typing, this is possible in a limited but 131 00:07:01.260 --> 00:07:02.620 useful number of cases. 132 00:07:02.620 --> 00:07:05.460 But as it turns out, this is one of those useful cases. 133 00:07:05.460 --> 00:07:08.980 Nesting dictionaries, as we've done here for the locations dictionary. 134 00:07:08.980 --> 00:07:09.840 Is quite advanced. 135 00:07:09.840 --> 00:07:13.140 So, congratulations if you've worked out the solution. 136 00:07:13.140 --> 00:07:16.750 And, because we're using string keys, lint's finding it hard to cope. 137 00:07:16.750 --> 00:07:18.580 Now, obviously the program runs, so 138 00:07:18.580 --> 00:07:20.500 the path in itself is quite happy with the syntax. 139 00:07:20.500 --> 00:07:22.660 So, it's more the lint checker here. 140 00:07:22.660 --> 00:07:25.840 Now incidentally, the reference to duck typing Is the path and 141 00:07:25.840 --> 00:07:26.910 why of doing things. 142 00:07:26.910 --> 00:07:29.910 As far as path is concerned, if it quacks like a duck, then it is a duck. 143 00:07:29.910 --> 00:07:34.630 So in other words, since path is dynamically typed, 144 00:07:34.630 --> 00:07:38.130 then we said that before, and it's more interested in the behavior of things 145 00:07:38.130 --> 00:07:41.480 than actually than being declared As a particular type. 146 00:07:41.480 --> 00:07:43.060 And that's a good thing. 147 00:07:43.060 --> 00:07:45.260 Because, of course, there's no type declarations in path. 148 00:07:45.260 --> 00:07:49.500 So the bottom line is that although the IntelliJ checker expects named exits, 149 00:07:49.500 --> 00:07:51.030 in this case, to be an integer index. 150 00:07:51.030 --> 00:07:52.570 And I'm talking about this part, here. 151 00:07:52.570 --> 00:07:53.890 For example, on line 51. 152 00:07:53.890 --> 00:07:57.390 So although it's expecting that and preferring that to be an integer index, 153 00:07:57.390 --> 00:08:01.130 Python itself knows that the location's like this component. 154 00:08:02.440 --> 00:08:03.680 It knows that that's a dictionary and 155 00:08:03.680 --> 00:08:07.620 it's happy to accept a string key into it because of the way we've defined it. 156 00:08:07.620 --> 00:08:11.540 So this is one of those sort of relatively rare cases, where the checker isn't really 157 00:08:11.540 --> 00:08:15.120 right in its explanations or when it's telling us that we've got something wrong. 158 00:08:15.120 --> 00:08:17.530 There isn't a problem with the way we've developed the code here. 159 00:08:17.530 --> 00:08:18.370 All right, so that's it. 160 00:08:18.370 --> 00:08:21.440 We're finally now done with dictionaries, and again, congratulations if you did 161 00:08:21.440 --> 00:08:26.420 figure this out because it's getting to be quite advanced code figuring out 162 00:08:26.420 --> 00:08:29.620 solutions to dictionaries within dictionaries as we've actually got here. 163 00:08:29.620 --> 00:08:31.920 So we're going to move on from dictionaries now. 164 00:08:31.920 --> 00:08:35.080 In the next video we're going to start work on sets. 165 00:08:35.080 --> 00:08:36.140 So, see you in that next video.