1
00:00:05,400 --> 00:00:07,290
Okay, so it's time for a challenge.

2
00:00:07,290 --> 00:00:10,120
This is the second challenge
with dictionaries.

3
00:00:10,120 --> 00:00:12,510
Now we've mentioned that the data for

4
00:00:12,510 --> 00:00:16,110
this adventure game could be
organized in many different ways.

5
00:00:16,110 --> 00:00:19,300
And we've created another way for
you here in this code.

6
00:00:19,300 --> 00:00:21,920
So your mission,
if you chose to accept it.

7
00:00:21,920 --> 00:00:24,240
Is to change the code
below to make it work.

8
00:00:24,240 --> 00:00:29,010
Now, the code that is below is
the complete program from the last video,

9
00:00:29,010 --> 00:00:33,470
the adventure game but
with the location's dictionary modified so

10
00:00:33,470 --> 00:00:37,050
that everything is in a single dictionary.

11
00:00:37,050 --> 00:00:40,270
So what your job here if the challenge
is to fix it ANd we'll go down and

12
00:00:40,270 --> 00:00:43,160
have a look at the code, and
you'll notice there are some errors here.

13
00:00:43,160 --> 00:00:45,170
There's errors there
that you'll need to fix.

14
00:00:45,170 --> 00:00:49,390
So you'll need to get this working because
we've not got our descriptions for

15
00:00:49,390 --> 00:00:52,050
the various locations, nox, x, etc.

16
00:00:52,050 --> 00:00:54,760
All in the one sort of master dictionary.

17
00:00:54,760 --> 00:00:55,410
So that's your goal.

18
00:00:55,410 --> 00:00:58,540
Now the thing to remember with this
challenge is there's not a lot of code you

19
00:00:58,540 --> 00:00:59,610
need to change.

20
00:00:59,610 --> 00:01:03,930
In fact you should only have to make
changes to just four lines of code to

21
00:01:03,930 --> 00:01:04,830
get this to work.

22
00:01:04,830 --> 00:01:08,040
Now check any of your warnings carefully
if you're using IntelliJ here.

23
00:01:08,040 --> 00:01:11,670
And don't worry if all four
changes give you the same warning.

24
00:01:11,670 --> 00:01:14,250
We will explain why, in the solution.

25
00:01:14,250 --> 00:01:16,680
I'm gonna ask you to pause the video now,
go away and

26
00:01:16,680 --> 00:01:18,360
see if you can solve this challenge.

27
00:01:18,360 --> 00:01:21,220
Once you're ready to come back and
see the solution, click play and

28
00:01:21,220 --> 00:01:22,400
I'll be here showing you how to do it.

29
00:01:25,580 --> 00:01:28,790
Okay so,
hopefully you managed to solve this.

30
00:01:28,790 --> 00:01:29,600
So, let's go through and

31
00:01:29,600 --> 00:01:34,300
show you our solution, and
how we would go about fixing this.

32
00:01:34,300 --> 00:01:38,470
So the first thing we need to change here,
the error is showing on line 43.

33
00:01:38,470 --> 00:01:43,910
So, clearly the problem there is it's
trying to use the exits dictionary,

34
00:01:43,910 --> 00:01:45,780
which no longer exists.

35
00:01:45,780 --> 00:01:47,600
So, we need to change the code.

36
00:01:47,600 --> 00:01:49,620
To use our master dictionary.

37
00:01:49,620 --> 00:01:52,830
So what need to do there is we still
need to join it, so availableExits,

38
00:01:52,830 --> 00:01:57,210
that first part is correct but the join
now is not exits rather it's on locations.

39
00:01:57,210 --> 00:02:03,300
So we type locations[loc] but
then We need to put

40
00:02:03,300 --> 00:02:07,990
exits to use the exits component
of the dictionary, and dot keys.

41
00:02:07,990 --> 00:02:12,080
So that's the first change,
pretty simple change as you can see.

42
00:02:12,080 --> 00:02:16,210
So we're looking up the key exits and
grabbing the keys for those.

43
00:02:16,210 --> 00:02:19,760
Which is now, of course,
all enclosed in one master dictionary.

44
00:02:19,760 --> 00:02:22,290
Of which we've got
subdictionaries built in.

45
00:02:22,290 --> 00:02:25,280
So we've still got a room location,
so you can see there,

46
00:02:25,280 --> 00:02:29,840
the number equated to each room,
but we've now got two entries.

47
00:02:29,840 --> 00:02:31,650
Before we had a single dictionary.

48
00:02:31,650 --> 00:02:35,940
We've got D E S C, which is
the description for the location and

49
00:02:35,940 --> 00:02:41,230
also got exits Which is our
valid exit in the named exits.

50
00:02:41,230 --> 00:02:45,230
So we've separated the three variables
that we had, three separate dictionaries

51
00:02:45,230 --> 00:02:47,990
and we put those dictionaries
into this one master dictionary.

52
00:02:47,990 --> 00:02:49,090
So each location,

53
00:02:49,090 --> 00:02:52,790
in essence, has now We got three
dictionaries as you can see there.

54
00:02:52,790 --> 00:02:53,870
So it's a little bit confusing.

55
00:02:53,870 --> 00:02:59,100
So we got one master as you can see there
which contains the description, the exits

56
00:02:59,100 --> 00:03:04,040
and the main exits and of course exits and
main exits are themselves dictionaries.

57
00:03:04,040 --> 00:03:08,530
So there's three dictionaries there in
total as we can see effectively for

58
00:03:08,530 --> 00:03:09,100
each location.

59
00:03:09,100 --> 00:03:10,760
So a little bit confusing.

60
00:03:10,760 --> 00:03:12,880
But that's the gist of it anyway.

61
00:03:12,880 --> 00:03:16,540
OK, so that was the first fix, so
now what we're doing is we're correctly

62
00:03:16,540 --> 00:03:19,510
looking up lock, which is finding
the relevant location number,

63
00:03:19,510 --> 00:03:23,000
then we're looking up the exits, so
we're using that lookup there to find that

64
00:03:23,000 --> 00:03:28,180
particular dictionary, then using the dot
keys to get all the keys that exist for

65
00:03:28,180 --> 00:03:31,380
that particular entry
This case which is exits.

66
00:03:31,380 --> 00:03:34,600
So that's the first fix, so
I'll just come back here again,

67
00:03:34,600 --> 00:03:39,260
it's got another problem there,
print locations, loc isn't going to work.

68
00:03:39,260 --> 00:03:42,580
What we need to do is also
use the descriptions.

69
00:03:42,580 --> 00:03:46,190
We need to look that up to make sure
we're grabbing it from the right spot.

70
00:03:46,190 --> 00:03:50,430
So I'd need to use that
key to retrieve it, so

71
00:03:50,430 --> 00:03:55,810
to do that, we just put, as you can see.

72
00:03:55,810 --> 00:04:00,250
And then that will now correctly find
us the description for the location.

73
00:04:01,520 --> 00:04:05,070
So that's two fixes,
now these errors here on line 50 and

74
00:04:05,070 --> 00:04:08,970
51 also have to be fixed, and in the first
one you'll probably guess that because

75
00:04:08,970 --> 00:04:14,010
it's very similar to what we did on line
43, so we're no longer using the Exits

76
00:04:14,010 --> 00:04:18,370
dictionary as a separate variable,
it's now part of our master dictionary So

77
00:04:18,370 --> 00:04:20,950
to fix that,
we'll change that to locations.

78
00:04:22,100 --> 00:04:25,320
Loc and then it's going to be exits.

79
00:04:27,760 --> 00:04:28,380
Like so.
And

80
00:04:28,380 --> 00:04:31,780
that's now grabbing the exits dictionary
and then taking a copy of that.

81
00:04:31,780 --> 00:04:34,930
And likewise, on the next line, you can
probably get that with named exits.

82
00:04:34,930 --> 00:04:37,360
We no longer have a separate variable for
that.

83
00:04:37,360 --> 00:04:39,150
So we need to fix that as well.

84
00:04:39,150 --> 00:04:42,270
So we're going to leave name exits in
because we're gonna use that part of it.

85
00:04:42,270 --> 00:04:48,870
But we're going to make it locations lock,
and

86
00:04:49,870 --> 00:04:55,750
then it's going to be,
named exits, like so.

87
00:04:57,150 --> 00:04:59,010
We can get rid of the rest
of that line now.

88
00:04:59,010 --> 00:05:01,900
And of course I made a typo there so
that shouldn't be in there.

89
00:05:01,900 --> 00:05:02,810
Let's fix that up.

90
00:05:02,810 --> 00:05:04,070
So that should now be it.

91
00:05:04,070 --> 00:05:06,770
That should be all we need to run this.

92
00:05:06,770 --> 00:05:08,510
So let's just run it and
make sure it works.

93
00:05:13,920 --> 00:05:17,440
We correctly not getting our numbers for
the available exits so that's good,

94
00:05:17,440 --> 00:05:18,550
that part's working.

95
00:05:18,550 --> 00:05:23,990
Then we can type go west, you're at
the top of a hill, we can go type in road.

96
00:05:23,990 --> 00:05:26,940
Can't go in that direction,
which is correct because of where we are,

97
00:05:26,940 --> 00:05:31,360
we can go north You're in the forest,
control, type in road again.

98
00:05:31,360 --> 00:05:34,680
You are standing at the end of a road
before a small brick building.

99
00:05:34,680 --> 00:05:35,920
We should be able to enter hill.

100
00:05:35,920 --> 00:05:37,970
You are at the top of a hill.

101
00:05:37,970 --> 00:05:40,410
And you can see the available
exits are working.

102
00:05:40,410 --> 00:05:41,500
Quit out of that.

103
00:05:41,500 --> 00:05:43,220
You are sitting at
a computer learning python.

104
00:05:43,220 --> 00:05:44,670
I'm going to close this down again.

105
00:05:46,410 --> 00:05:47,480
And we could also put in here,

106
00:05:47,480 --> 00:05:50,720
if we wanted to,
plus just a space there if we wanted to.

107
00:05:50,720 --> 00:05:51,590
So let me run it.

108
00:05:52,680 --> 00:05:54,770
We've now got that extra space there.

109
00:05:54,770 --> 00:05:56,480
So that's the challenge.

110
00:05:56,480 --> 00:05:57,580
Clean up the errors now.

111
00:05:57,580 --> 00:06:01,040
The lint checker in IntelliJ is
complaining, and PyCharm will do the same.

112
00:06:01,040 --> 00:06:01,940
It's gonna show a warning.

113
00:06:01,940 --> 00:06:07,300
So if we come over here and select them,
expected type int matching generic types.

114
00:06:07,300 --> 00:06:09,520
And we'll just click on that to
see which line it's talking about.

115
00:06:09,520 --> 00:06:11,350
You can see it's line 43.

116
00:06:11,350 --> 00:06:15,730
I should put an arrow fix in
there to fix it up, like so.

117
00:06:15,730 --> 00:06:18,720
And the other arrows relate to the two
other lines that we actually worked on.

118
00:06:18,720 --> 00:06:22,370
And you can see it's having the same
problems as you can see there.

119
00:06:22,370 --> 00:06:26,700
So these are the four lines that
we've modified that Intelliga is

120
00:06:26,700 --> 00:06:30,530
saying coming up with a problem,
potentially a problem showing a warning.

121
00:06:30,530 --> 00:06:34,800
So the problem is it's complaining that it
expected an int, but got a string instead.

122
00:06:34,800 --> 00:06:38,930
So generally speaking, the lint checker,
and that's what this process is,

123
00:06:38,930 --> 00:06:41,810
does a really good job of checking
your code as you type it.

124
00:06:41,810 --> 00:06:43,610
But that said, it's not perfect.

125
00:06:43,610 --> 00:06:48,020
So the hint does say to press control F1,
command F1 on a Mac.

126
00:06:48,020 --> 00:06:51,760
And we get the full message, and
basically due to dynamic dispatch and

127
00:06:51,760 --> 00:06:56,390
duck typing, This is possible in
a limited, but useful, number of cases.

128
00:06:56,390 --> 00:06:58,660
Now [LAUGH] that's a bit
of a cryptic message and

129
00:06:58,660 --> 00:07:00,270
I'll just bring that up
on the screen again.

130
00:07:00,270 --> 00:07:04,320
Again, due to dynamic dispatch and duck
typing, this is possible in a limited but

131
00:07:04,320 --> 00:07:05,680
useful number of cases.

132
00:07:05,680 --> 00:07:08,520
But as it turns out,
this is one of those useful cases.

133
00:07:08,520 --> 00:07:12,040
Nesting dictionaries, as we've done
here for the locations dictionary.

134
00:07:12,040 --> 00:07:12,900
Is quite advanced.

135
00:07:12,900 --> 00:07:16,200
So, congratulations if you've
worked out the solution.

136
00:07:16,200 --> 00:07:19,810
And, because we're using string keys,
lint's finding it hard to cope.

137
00:07:19,810 --> 00:07:21,640
Now, obviously the program runs, so

138
00:07:21,640 --> 00:07:23,560
the path in itself is quite
happy with the syntax.

139
00:07:23,560 --> 00:07:25,720
So, it's more the lint checker here.

140
00:07:25,720 --> 00:07:28,900
Now incidentally, the reference
to duck typing Is the path and

141
00:07:28,900 --> 00:07:29,970
why of doing things.

142
00:07:29,970 --> 00:07:32,970
As far as path is concerned, if it
quacks like a duck, then it is a duck.

143
00:07:32,970 --> 00:07:37,690
So in other words,
since path is dynamically typed,

144
00:07:37,690 --> 00:07:41,190
then we said that before, and it's more
interested in the behavior of things

145
00:07:41,190 --> 00:07:44,540
than actually than being
declared As a particular type.

146
00:07:44,540 --> 00:07:46,120
And that's a good thing.

147
00:07:46,120 --> 00:07:48,320
Because, of course,
there's no type declarations in path.

148
00:07:48,320 --> 00:07:52,560
So the bottom line is that although
the IntelliJ checker expects named exits,

149
00:07:52,560 --> 00:07:54,090
in this case, to be an integer index.

150
00:07:54,090 --> 00:07:55,630
And I'm talking about this part, here.

151
00:07:55,630 --> 00:07:56,950
For example, on line 51.

152
00:07:56,950 --> 00:08:00,450
So although it's expecting that and
preferring that to be an integer index,

153
00:08:00,450 --> 00:08:04,190
Python itself knows that
the location's like this component.

154
00:08:05,500 --> 00:08:06,740
It knows that that's a dictionary and

155
00:08:06,740 --> 00:08:10,680
it's happy to accept a string key into
it because of the way we've defined it.

156
00:08:10,680 --> 00:08:14,600
So this is one of those sort of relatively
rare cases, where the checker isn't really

157
00:08:14,600 --> 00:08:18,180
right in its explanations or when it's
telling us that we've got something wrong.

158
00:08:18,180 --> 00:08:20,590
There isn't a problem with the way
we've developed the code here.

159
00:08:20,590 --> 00:08:21,430
All right, so that's it.

160
00:08:21,430 --> 00:08:24,500
We're finally now done with dictionaries,
and again, congratulations if you did

161
00:08:24,500 --> 00:08:29,480
figure this out because it's getting
to be quite advanced code figuring out

162
00:08:29,480 --> 00:08:32,679
solutions to dictionaries within
dictionaries as we've actually got here.

163
00:08:32,679 --> 00:08:34,980
So we're going to move on
from dictionaries now.

164
00:08:34,980 --> 00:08:38,140
In the next video we're
going to start work on sets.

165
00:08:38,140 --> 00:08:39,200
So, see you in that next video.

