1
00:00:05,320 --> 00:00:08,980
So I continue on with that discussion
of dictionaries in Python,

2
00:00:08,980 --> 00:00:11,260
that we've worked on in
the last two videos.

3
00:00:11,260 --> 00:00:14,510
Again, just recapping what we
did in these last few lines.

4
00:00:14,510 --> 00:00:16,640
We had fruit and it's called keys.

5
00:00:16,640 --> 00:00:18,730
So we got a view object.

6
00:00:18,730 --> 00:00:20,780
When we use the dot keys,
that's what we get.

7
00:00:20,780 --> 00:00:24,030
We get a special type, sort of like
a list, but it's a view object.

8
00:00:24,030 --> 00:00:26,960
We then printed that out so you can
see it on the screen Then we add it

9
00:00:26,960 --> 00:00:30,150
a new item to the dictionary and when
we print it out that fruit case again,

10
00:00:30,150 --> 00:00:34,980
even though we haven't directly updated
that variable, it was updated by Python.

11
00:00:34,980 --> 00:00:36,400
So it happened behind the scenes.

12
00:00:36,400 --> 00:00:39,040
You can see the tomato was
added in the second example.

13
00:00:39,040 --> 00:00:42,270
That again is the only way that
view objects can be updated.

14
00:00:42,270 --> 00:00:46,090
Sort of indirectly with the dictionary
that's happening behind the scenes.

15
00:00:46,090 --> 00:00:49,440
So we can't add items, and
using delete we get an error here.

16
00:00:49,440 --> 00:00:52,600
What we can do is we can
convert them to lists.

17
00:00:52,600 --> 00:00:56,680
So we can convert them to lists, and
you saw us using the list method.

18
00:00:56,680 --> 00:00:58,220
The list function I should say there list.

19
00:00:58,220 --> 00:01:01,210
And we were quite able to
actually convert that to list and

20
00:01:01,210 --> 00:01:03,130
then use the sort function to sort them.

21
00:01:03,130 --> 00:01:07,210
But before we leave our fruit and move on
there's one more view we should look at

22
00:01:07,210 --> 00:01:11,550
and that's the items method which returns
a dict_items object, which

23
00:01:11,550 --> 00:01:15,690
is another dynamic view object that
consists of key and value tuples as well.

24
00:01:15,690 --> 00:01:18,030
So to do that We'll get rid of this code.

25
00:01:19,360 --> 00:01:23,150
Cut that out.
Let's close out the run window for now.

26
00:01:24,280 --> 00:01:27,040
And we'll put the print in.

27
00:01:27,040 --> 00:01:27,640
Print fruit.

28
00:01:28,910 --> 00:01:32,190
We can do printfruit.items.

29
00:01:33,990 --> 00:01:35,590
Oops, semicolon there, wrong language.

30
00:01:35,590 --> 00:01:39,350
[LAUGH] Run that, and
you can see we've got a different output.

31
00:01:39,350 --> 00:01:45,230
It looks very similar if you
recall our videos on tuples.

32
00:01:45,230 --> 00:01:49,190
And as it turns out, we can produce
a tuple from it using our tuple function.

33
00:01:49,190 --> 00:01:54,580
So we could do f_tuple equals tuple, they

34
00:01:55,790 --> 00:02:00,580
give us a built in function, fruit.items,

35
00:02:00,580 --> 00:02:06,070
and print f_tuple, like so.

36
00:02:06,070 --> 00:02:11,890
We can run that, and I forgot to put some
brackets in there, cuz it's a method call.

37
00:02:11,890 --> 00:02:15,310
Run that And on the last line now
you can see we have a regular tuple.

38
00:02:15,310 --> 00:02:19,760
And of course, from that point
on we can then use tuple, or

39
00:02:19,760 --> 00:02:25,060
in the case the variable tuple just
like we used tuples in any early videos.

40
00:02:25,060 --> 00:02:29,120
For example,
we could do something like for snack N

41
00:02:31,350 --> 00:02:37,430
quatupple item common description,

42
00:02:37,430 --> 00:02:46,290
equals snack print item
plus is past description.

43
00:02:46,290 --> 00:02:50,440
And we can run that, and
you can see we've got our items there.

44
00:02:50,440 --> 00:02:51,940
What was the key from the dictionary, and

45
00:02:51,940 --> 00:02:55,620
also the value from the dictionary
because we've created a topple.

46
00:02:55,620 --> 00:02:58,340
We've then got them in our
description variables.

47
00:02:58,340 --> 00:03:00,890
Now this wouldn't be Python if it
wasn't possible to go the other way.

48
00:03:00,890 --> 00:03:05,970
In other words to create a dictionary
from topples of key values pairs, and

49
00:03:05,970 --> 00:03:08,530
the dict constructor
allows us to do just that.

50
00:03:08,530 --> 00:03:13,550
So I walk down here On this
line we could type print.

51
00:03:13,550 --> 00:03:17,090
Then use the built-in dict function and

52
00:03:17,090 --> 00:03:19,360
pass a tuple to it,
the tuple that we've created.

53
00:03:20,520 --> 00:03:27,540
If you run that As you can see that there,
that's now just our regular dictionary,

54
00:03:27,540 --> 00:03:30,790
probably not terribly exciting to you
because all we've really done is got

55
00:03:30,790 --> 00:03:35,880
the original directory back
that we had that we created.

56
00:03:35,880 --> 00:03:36,740
On line one.

57
00:03:36,740 --> 00:03:40,610
But what it does do is demonstrate how
a dictionary can be created from a tuple

58
00:03:40,610 --> 00:03:42,260
which is extremely useful.

59
00:03:42,260 --> 00:03:46,360
There is a lot of interaction between
dictionaries, tuples and lists.

60
00:03:46,360 --> 00:03:48,210
And later in the course we'll
summarize everything we've

61
00:03:48,210 --> 00:03:49,400
done with these data structures.

62
00:03:49,400 --> 00:03:53,290
And what we'll also do is include a list
of all the methods that apply to each one.

63
00:03:53,290 --> 00:03:55,360
And now moving onto something
that's completely different,

64
00:03:55,360 --> 00:03:56,990
I'll just close down that one window.

65
00:03:56,990 --> 00:03:59,180
Now rather than try to cover
everything at once, and

66
00:03:59,180 --> 00:04:02,460
have it all become terribly overwhelming,
what we've been doing is we've been

67
00:04:02,460 --> 00:04:06,680
introducing methods and functions as
it becomes appropriate to use them.

68
00:04:06,680 --> 00:04:10,910
So now's a good time to introduce another
of the string methods Which is joined.

69
00:04:10,910 --> 00:04:14,610
By now you should be used to the idea
of mutable and immutable objects.

70
00:04:14,610 --> 00:04:16,519
If you remember strings are immutable.

71
00:04:16,519 --> 00:04:19,690
As a result this means that it's
not at all efficient to concatenate

72
00:04:19,690 --> 00:04:21,399
strings in a loop.

73
00:04:21,399 --> 00:04:26,260
Anytime you concatenate a string to an
existing string, the new string has to be

74
00:04:26,260 --> 00:04:30,780
created and if that's done in a loop
it becomes expensive and slow.

75
00:04:30,780 --> 00:04:32,910
So you really do want to avoid that.

76
00:04:32,910 --> 00:04:35,740
The string join method is
there to actually help you.

77
00:04:35,740 --> 00:04:39,620
It actually takes a sequence and
produces a string from it.

78
00:04:39,620 --> 00:04:42,500
So the items in the new
string are separated by

79
00:04:42,500 --> 00:04:44,350
the stream that join was called upon.

80
00:04:44,350 --> 00:04:46,990
So as said a lot of these things are
probably easier to demonstrate rather then

81
00:04:46,990 --> 00:04:48,200
for me to explain it any further.

82
00:04:48,200 --> 00:04:53,190
So I'm gonna create a new file now
to leave the [INAUDIBLE] as it was.

83
00:04:53,190 --> 00:04:53,860
New Python file.

84
00:04:53,860 --> 00:04:58,250
And we'll just call this one join for
want of a better word.

85
00:04:58,250 --> 00:05:00,480
So let's create a list first.

86
00:05:00,480 --> 00:05:07,340
And myList = ["a","b","c","d"].

87
00:05:07,340 --> 00:05:10,870
newString = "",
just an empty string for now.

88
00:05:11,910 --> 00:05:16,470
And we're going to go through,
iterate through my list.

89
00:05:16,470 --> 00:05:21,580
We'll see in my list newString += c + ",
" like so, and

90
00:05:21,580 --> 00:05:27,140
then we'll print(newString).

91
00:05:27,140 --> 00:05:29,170
So I consider this first example, and

92
00:05:29,170 --> 00:05:33,320
I will just make sure I'm running
this right one, running join.

93
00:05:33,320 --> 00:05:35,730
ABCD so
you can see has appeared on the screen.

94
00:05:35,730 --> 00:05:38,840
So as I mentioned this is not
a good way of producing code.

95
00:05:38,840 --> 00:05:40,260
So, a couple of problems here,

96
00:05:40,260 --> 00:05:43,360
leaving aside the fact firstly there's
a comma on the end of our output.

97
00:05:43,360 --> 00:05:47,530
It's also inefficient because every
time through the loop it's creating

98
00:05:47,530 --> 00:05:52,520
a new copy of new string because again the
strings that we're using are immutable.

99
00:05:52,520 --> 00:05:56,410
Now augmented assignment will help We did
say that talked about that earlier in this

100
00:05:56,410 --> 00:06:00,090
course it does try to update
the variable in place where possible but

101
00:06:00,090 --> 00:06:03,170
this is not one of those times where its
possible so in other words because strings

102
00:06:03,170 --> 00:06:06,990
are unbeatable augmented a summit isn't
gonna help you so what you'd want

103
00:06:06,990 --> 00:06:11,850
to do in this scenario is you want to use
join to fix this problem and the way we

104
00:06:11,850 --> 00:06:16,440
would use that Instead of putting
plus equals we will delete that out.

105
00:06:16,440 --> 00:06:20,720
We put newString = ", ".join(myList).

106
00:06:20,720 --> 00:06:21,470
And we run that.

107
00:06:23,020 --> 00:06:29,100
Notice we haven't got
the trailing comma now.

108
00:06:29,100 --> 00:06:30,760
So is much more efficient.

109
00:06:30,760 --> 00:06:33,500
It also solves the problem of
the trailing comme, as you can see there.

110
00:06:33,500 --> 00:06:36,780
And it's really much more Pythonic in that

111
00:06:36,780 --> 00:06:40,970
it's working in a way that Python would
intend to work for something like this.

112
00:06:40,970 --> 00:06:44,890
So the way this particular method works
is it creates a new string by taking each

113
00:06:44,890 --> 00:06:48,360
item in a sequence which is a list as
you can see that are created on line 1.

114
00:06:48,360 --> 00:06:53,480
And separates the item with a string
comma that join is actually invoked on.

115
00:06:53,480 --> 00:06:56,430
So we can use any sequence tied
by some parameter to join.

116
00:06:56,430 --> 00:06:59,560
So for example instead of a list
we can pass another string.

117
00:06:59,560 --> 00:07:01,350
So I'm just gonna place some code in here.

118
00:07:01,350 --> 00:07:03,000
You see what I mean.

119
00:07:03,000 --> 00:07:08,210
So let's change that to letters.

120
00:07:08,210 --> 00:07:10,130
And instead of my list, make that letters.

121
00:07:10,130 --> 00:07:13,630
Of course, you can see See the letters
there is now a string instead of a list.

122
00:07:13,630 --> 00:07:17,480
If we run that then we get
the same output as you can see.

123
00:07:17,480 --> 00:07:21,450
The same processing, I've since gone
from a to Zed because the input was

124
00:07:21,450 --> 00:07:25,400
a string in this case
containing the letters a to z.

125
00:07:25,400 --> 00:07:28,010
The other thing to remember is that the
string we called joined method on can be

126
00:07:28,010 --> 00:07:29,800
quite complex as well.

127
00:07:29,800 --> 00:07:33,670
If I for example,
come back here or close the again.

128
00:07:35,350 --> 00:07:37,880
Numbers, let's try instead of
just putting a comma, we'll put

129
00:07:42,070 --> 00:07:46,280
And then .join numbers.

130
00:07:46,280 --> 00:07:50,910
Run that, you see one Mississippi,
two Mississippi, three Mississippi.

131
00:07:50,910 --> 00:07:55,810
And so I probably to fix the Spacing,
then again you can say 1 Mississippi,

132
00:07:55,810 --> 00:07:58,700
2 Mississippi, 3 Mississippi, and so on.

133
00:07:58,700 --> 00:08:00,150
Again in case you're wondering why,

134
00:08:00,150 --> 00:08:02,760
separating numbers with the word
Mississippi is a very rough way to count

135
00:08:02,760 --> 00:08:05,420
seconds, if you don't
have a stopwatch handy.

136
00:08:05,420 --> 00:08:07,560
And you could also use 2000
in place of Mississippi.

137
00:08:07,560 --> 00:08:09,110
But anyway I'm getting off track there.

138
00:08:09,110 --> 00:08:11,900
You might have guessed that we're about to
concatenate the keys of a dictionary to

139
00:08:11,900 --> 00:08:13,600
give us a stream and you'd be right.

140
00:08:13,600 --> 00:08:17,010
So let's get to doing that because of
course this is all about dictionaries.

141
00:08:17,010 --> 00:08:20,340
What we're gonna do is we're going to use
dictionaries to store locations in a very

142
00:08:20,340 --> 00:08:21,850
basic adventure game.

143
00:08:21,850 --> 00:08:24,920
We'll also store the available exits in
another dictionary and we're gonna link

144
00:08:24,920 --> 00:08:28,790
the two together so that players can
move from one location to another.

145
00:08:28,790 --> 00:08:30,870
So I'm just gonna bring a little
image up on the screen now so

146
00:08:30,870 --> 00:08:32,630
you can see what I'm talking about.

147
00:08:33,750 --> 00:08:35,929
You can see there that there
is five locations a road,

148
00:08:35,929 --> 00:08:38,950
a hill, a building,
a valley, and a forest.

149
00:08:38,950 --> 00:08:40,750
The roads are not all straight and
you can see this.

150
00:08:40,750 --> 00:08:44,590
That going north from the hill takes you
to the forest entering from the west.

151
00:08:44,590 --> 00:08:46,630
So you can go west from the forest
to get back to the hill.

152
00:08:46,630 --> 00:08:51,100
What we are going to do What we can do is
we can represent the map as a dictionary.

153
00:08:51,100 --> 00:08:54,530
And although we could use the short
names of the location as the key,

154
00:08:54,530 --> 00:08:57,810
what we're gonna do is use numbers instead
cuz that's easy to read initially.

155
00:08:57,810 --> 00:09:00,370
And if you want to replace one
with rho etc., a simple search and

156
00:09:00,370 --> 00:09:02,540
replace will do that once
everything's working.

157
00:09:02,540 --> 00:09:06,590
So what I'm going to do now is just copy
and paste that code for that dictionary.

158
00:09:07,960 --> 00:09:09,580
I'm just going to replace
the code we worked on here.

159
00:09:09,580 --> 00:09:13,790
So I'm gonna paste it in there like so,
and you can see we've got the 5 locations

160
00:09:13,790 --> 00:09:17,940
and the keys you can see for each
value in the dictionary is the number,

161
00:09:17,940 --> 00:09:20,690
and then it's obviously the description,
which is the, and

162
00:09:20,690 --> 00:09:23,790
the other one, the other part is
the value as well for the dictionary.

163
00:09:23,790 --> 00:09:25,130
So you've got the key and the value for

164
00:09:25,130 --> 00:09:27,650
each and the 5 locations on
the screen as you can see there.

165
00:09:27,650 --> 00:09:30,390
Incidentally, I think that's a lot easier
to do when you're adding dictionary

166
00:09:30,390 --> 00:09:33,130
entries like this, creating a dictionary,
it is better to separate them.

167
00:09:33,130 --> 00:09:35,670
So I put a comma and separate them on
a separate them on a separate line.

168
00:09:35,670 --> 00:09:39,210
It's just easier to read and sort of
avoids your dictionaries becoming a little

169
00:09:39,210 --> 00:09:42,450
bit too messy as can happen if you've got
them all sort of linked up on a line.

170
00:09:43,530 --> 00:09:45,950
You can see I did it in there
as well with the fruit.

171
00:09:45,950 --> 00:09:48,380
So what we now need to do,
we've got our basic locations,

172
00:09:48,380 --> 00:09:51,560
we need to represent the available
exits from each location.

173
00:09:51,560 --> 00:09:55,480
Comparing to mine again is four plus one
exits, north, south, east, and west,

174
00:09:55,480 --> 00:09:57,360
plus quit to exit the game.

175
00:09:57,360 --> 00:09:59,200
So quit's going to available everywhere,

176
00:09:59,200 --> 00:10:01,070
otherwise the available
exits will match the map.

177
00:10:01,070 --> 00:10:05,560
So again, looking at the map again
What we;re trying to do now is

178
00:10:05,560 --> 00:10:09,890
basically duplicating this set of
options which direction is valid.

179
00:10:09,890 --> 00:10:10,580
A set, but

180
00:10:10,580 --> 00:10:15,100
also enabling a quit to actually enable
you to quit the game at any point.

181
00:10:15,100 --> 00:10:17,850
From the road for example,
we can go in all four directions, but

182
00:10:17,850 --> 00:10:19,730
if we go east to the building.

183
00:10:19,730 --> 00:10:22,550
The only exit from there is
gonna be ways back to road.

184
00:10:22,550 --> 00:10:25,100
So one way we can represent
this would be a dictionary with

185
00:10:25,100 --> 00:10:27,030
the directions being the key and

186
00:10:27,030 --> 00:10:31,450
the values being the location that we
end up at if we go in that direction.

187
00:10:31,450 --> 00:10:34,370
So from one,
remember in our key one is the road,

188
00:10:34,370 --> 00:10:38,490
if you go west from there,
we'll end up at two, which is the hill.

189
00:10:38,490 --> 00:10:41,870
And if you go east We get to three,
the building.

190
00:10:41,870 --> 00:10:45,100
So when we get to a new location,
we should print out the description.

191
00:10:45,100 --> 00:10:47,840
And that's gonna be easy to
do because each direction

192
00:10:47,840 --> 00:10:52,040
gives us the key of the destination in the
locations directory, if that makes sense.

193
00:10:52,040 --> 00:10:53,780
So what I'm gonna do is paste this in now.

194
00:10:53,780 --> 00:10:59,200
Our next dictionary is going to be exits,
like so.

195
00:10:59,200 --> 00:11:02,430
And you can see now that we're
using a key for the direction And

196
00:11:02,430 --> 00:11:05,960
the value is of a key from the locations.

197
00:11:05,960 --> 00:11:09,480
And again, the reason we're doing that
is so that we can ultimately print out

198
00:11:09,480 --> 00:11:12,850
the location when we move,
where we've moved to in other words.

199
00:11:12,850 --> 00:11:14,840
So again, bringing the image
back up on the screen again.

200
00:11:14,840 --> 00:11:18,780
So if we start off on the road, we get
the available exits by looking at index 1.

201
00:11:18,780 --> 00:11:21,270
So I'm just going to
just put that over there.

202
00:11:21,270 --> 00:11:25,090
Again the exits is represented
as a list of dictionary objects.

203
00:11:25,090 --> 00:11:27,360
This contains one dictionary for
each location.

204
00:11:27,360 --> 00:11:30,310
Notice this is a list
of dictionary objects.

205
00:11:30,310 --> 00:11:33,920
Important distinction to locations
which is just a dictionary.

206
00:11:33,920 --> 00:11:37,490
And we're doing it this way ultimately
because we can index the list.

207
00:11:37,490 --> 00:11:39,260
We know that element zero is
always going to be the same.

208
00:11:39,260 --> 00:11:39,830
We know that element.

209
00:11:39,830 --> 00:11:43,040
One's always going to be the same and
so on and so fourth unlike a dictionary.

210
00:11:43,040 --> 00:11:46,960
And the method we come up here isn't
necessarily the best way to represent

211
00:11:46,960 --> 00:11:47,880
the exists.

212
00:11:47,880 --> 00:11:51,850
For example, we could use a dictionary
with the key being the location number and

213
00:11:51,850 --> 00:11:55,070
the exists perhaps the List of tuples or
another dictionary.

214
00:11:55,070 --> 00:11:59,330
But it's probably easy to start this way,
and sort of progress to that ladder.

215
00:11:59,330 --> 00:12:02,020
So again, if we start off on the right,
we can get the available width just by

216
00:12:02,020 --> 00:12:05,070
looking at index one in
the list to get the dictionary.

217
00:12:05,070 --> 00:12:06,540
So again, we start here.

218
00:12:06,540 --> 00:12:09,380
One, come over here to
element one on the list.

219
00:12:09,380 --> 00:12:12,660
And, that's the valid entries,
or the valid directions and

220
00:12:12,660 --> 00:12:15,630
also the quick to exit for
that particular location.

221
00:12:15,630 --> 00:12:19,880
So, standing on the road, going west would
go to two, location two which is the hill.

222
00:12:19,880 --> 00:12:21,240
East would go to three.

223
00:12:21,240 --> 00:12:22,550
Which is a Building.

224
00:12:22,550 --> 00:12:24,330
North will go to five
which is the Forest and

225
00:12:24,330 --> 00:12:26,520
south would go t four which is a Valley.

226
00:12:26,520 --> 00:12:29,600
And again, we have the Q coming
out to quit out which is going to

227
00:12:29,600 --> 00:12:31,890
basically quit the game.

228
00:12:31,890 --> 00:12:37,080
Similarly from the Hill, we get the
dictionary in position two on the list.

229
00:12:37,080 --> 00:12:39,850
You can see position two has
been defined there on line 10.

230
00:12:39,850 --> 00:12:42,520
The only way we can go from the hill,
we can go north to go to 5,

231
00:12:42,520 --> 00:12:44,010
which of course is the forest.

232
00:12:44,010 --> 00:12:47,400
There's no other exit, other than quit,
so we can't get back to the road from

233
00:12:47,400 --> 00:12:48,750
the hill,
that's the way we've designed the game.

234
00:12:48,750 --> 00:12:52,020
You can see that by the arrows telling
you whether it's multi-directional or

235
00:12:52,020 --> 00:12:53,260
single directional.

236
00:12:53,260 --> 00:12:56,310
So now that we've done that,
a simple loop should allow us

237
00:12:56,310 --> 00:12:59,770
to allow the player to keep entering
directions until it's [INAUDIBLE].

238
00:12:59,770 --> 00:13:02,510
And each time around the loop,
we'll display descriptions of the local in

239
00:13:02,510 --> 00:13:05,420
the available exits,
just to make it look good.

240
00:13:05,420 --> 00:13:06,330
So let's try that.

241
00:13:08,210 --> 00:13:12,640
So I'm gonna type [INAUDIBLE]
equals one,

242
00:13:12,640 --> 00:13:19,030
while true, and continue until,
basically till Our quitters are selected

243
00:13:19,030 --> 00:13:23,880
available exits equals nothing for now.

244
00:13:23,880 --> 00:13:31,900
For direction in exits location
we're starting off at location one.

245
00:13:31,900 --> 00:13:35,170
Again we're starting off
starting on the road, number one,

246
00:13:35,170 --> 00:13:36,500
that's why I like lock equals one.

247
00:13:38,450 --> 00:13:43,650
So retrieving the dictionary from
our list as you can see there for

248
00:13:43,650 --> 00:13:46,590
that particular direction,
we're getting the directions and

249
00:13:46,590 --> 00:13:49,620
available booting up our list then,
available exits.

250
00:13:49,620 --> 00:13:53,270
Plus equals direction.

251
00:13:55,700 --> 00:14:02,300
Like so.
And then we're going to print locations

252
00:14:02,300 --> 00:14:11,300
and loc is equal to zero.

253
00:14:11,300 --> 00:14:11,800
Oops.

254
00:14:17,130 --> 00:14:21,890
Don't need a bracket there.

255
00:14:21,890 --> 00:14:28,870
Then I'm going to type direction
equals input available.

256
00:14:28,870 --> 00:14:34,970
[INAUDIBLE]
plus available exits Or upper.

257
00:14:34,970 --> 00:14:38,820
[NOISE]
Just going to do an empty print.

258
00:14:38,820 --> 00:14:45,590
[NOISE]
If direction in exits.

259
00:14:45,590 --> 00:14:49,690
[NOISE]
Fixes in such a way,

260
00:14:52,580 --> 00:14:57,950
and lock equals exists so in other
words if it's a valid location and

261
00:15:00,000 --> 00:15:03,970
we're gonna sign that direction.

262
00:15:05,140 --> 00:15:13,360
Else, print,
You cannot go in that direction.

263
00:15:16,780 --> 00:15:20,340
The program's gonna keep looping until
the player goes to location zero.

264
00:15:20,340 --> 00:15:23,340
At that point the programs gonna
break out as you can see there

265
00:15:23,340 --> 00:15:24,990
on line 24 when the game ends.

266
00:15:24,990 --> 00:15:29,310
So when a direction is chosen we check
that the k exists in the appropriate

267
00:15:29,310 --> 00:15:31,030
dictionary from our list of exits.

268
00:15:31,030 --> 00:15:32,680
And you can see we were doing that.

269
00:15:32,680 --> 00:15:37,350
If direction in exits location or
lock, I should say the lock variable.

270
00:15:37,350 --> 00:15:41,270
And then we use the key to retrieve the
next location if it does actually exist.

271
00:15:41,270 --> 00:15:44,850
The exits lock and direction.

272
00:15:44,850 --> 00:15:47,880
Again we are using that as
an index into the exits list

273
00:15:47,880 --> 00:15:51,990
to retrieve the dictionary containing
the available exits of that location.

274
00:15:51,990 --> 00:15:55,300
So when we get to equals one location,
one gives the description,

275
00:15:55,300 --> 00:15:59,280
you are standing at the end of the rope,
you cross groups, small brick building and

276
00:15:59,280 --> 00:16:00,090
we can actually run that.

277
00:16:01,630 --> 00:16:04,710
So I can see [INAUDIBLE] standing
at the end of a rope before a small

278
00:16:04,710 --> 00:16:05,710
brick building.

279
00:16:05,710 --> 00:16:10,610
It shows us we can go east,
south, north, quit or west.

280
00:16:10,610 --> 00:16:11,280
I can just type quit.

281
00:16:11,280 --> 00:16:14,450
You're sitting at a computer,
learning [INAUDIBLE] I should say,

282
00:16:14,450 --> 00:16:16,060
then it's exited the game as you can see.

283
00:16:16,060 --> 00:16:21,050
If we run that again This time off
we go south we're gonna valley

284
00:16:22,330 --> 00:16:26,670
just to confirm that it's
south toward valley, so

285
00:16:26,670 --> 00:16:34,560
we shouldn't have to go back towards
north so we shouldn't have to go west.

286
00:16:37,120 --> 00:16:41,320
At the top of a hill we go back
to west was the hill again and

287
00:16:41,320 --> 00:16:44,320
we shouldn't now be able to go back to the
valley because the way the program's been

288
00:16:44,320 --> 00:16:47,410
designed, we should only be able to go
to the forest now which would be north.

289
00:16:49,320 --> 00:16:53,220
North and quit, north, you're at a forest.

290
00:16:55,270 --> 00:16:58,250
But the forest and from there we should
be able to go back to the road or

291
00:16:58,250 --> 00:17:01,900
back to the hill so
it should either be a south or west.

292
00:17:03,060 --> 00:17:06,180
South or west as you can see there and
so on and so forth.

293
00:17:06,180 --> 00:17:06,890
We can quit out of that.

294
00:17:07,990 --> 00:17:10,720
So I'm gonna close that run window,
and again,

295
00:17:10,720 --> 00:17:15,300
booting up the list in booting
up a list of compatible exits,

296
00:17:15,300 --> 00:17:18,450
we're iterating through the keys
of the dictionary to do that.

297
00:17:18,450 --> 00:17:21,510
But wait a minute,
didn't we just say earlier in this video

298
00:17:21,510 --> 00:17:24,480
That can cannonading strings
in a loop wasn't a good idea.

299
00:17:24,480 --> 00:17:27,329
Without the moment it's probably
not a problem because let's face it

300
00:17:27,329 --> 00:17:29,590
there's a maximum five
keys in the dictionary.

301
00:17:29,590 --> 00:17:32,620
When this game becomes the most popular
multiplayer game on the internet and

302
00:17:32,620 --> 00:17:35,490
we've got thousands of people
playing it in real time, that

303
00:17:35,490 --> 00:17:39,180
performance hit will become significant
and things will slow probably to a call.

304
00:17:39,180 --> 00:17:44,070
So we can actually replace line 17
through 19 literally with one line.

305
00:17:44,070 --> 00:17:49,550
Let's go and do that,
we'll put available x is equals dot join,

306
00:17:49,550 --> 00:17:54,600
exits lock dot

307
00:17:54,600 --> 00:17:59,590
keys which you can see is far more
efficient has less lines of code to type.

308
00:17:59,590 --> 00:18:03,670
And are not going to be slowed down by
that problem we would have trying to

309
00:18:03,670 --> 00:18:04,260
strings.

310
00:18:04,260 --> 00:18:09,250
And we eventually have a huge, really
fast application and popular application.

311
00:18:09,250 --> 00:18:10,760
And you can see it should still work.

312
00:18:10,760 --> 00:18:12,010
We still get the same results.

313
00:18:12,010 --> 00:18:12,830
Here we can go North.

314
00:18:14,490 --> 00:18:19,090
The forest of [INAUDIBLE] he's sitting
in front of a computer so that's good.

315
00:18:19,090 --> 00:18:22,310
Of course the other thing we managed
to do by doing that approach was we got

316
00:18:22,310 --> 00:18:25,670
rid of the trailing comma,
which is good as well.

317
00:18:25,670 --> 00:18:30,010
You might be wondering, we use direction,
why didn't we use the word exit.

318
00:18:30,010 --> 00:18:33,040
Probably make more sense to say
the x at the exit variable there,

319
00:18:33,040 --> 00:18:33,920
because that's really what it is.

320
00:18:33,920 --> 00:18:36,580
We're trying to find the exit
to the current location.

321
00:18:36,580 --> 00:18:39,570
The reason is x is the key within python,
quite a dramatic one as well.

322
00:18:39,570 --> 00:18:41,900
Of course, [INAUDIBLE] quit immediately.

323
00:18:41,900 --> 00:18:43,930
So that's why we use direction instead.

324
00:18:43,930 --> 00:18:47,040
And incidentally this is a good
example of when you would check for

325
00:18:47,040 --> 00:18:50,390
the existence of a key rather than
using get with the default value.

326
00:18:50,390 --> 00:18:53,300
If we'd use get here, you remember we
talked about this in the previous video,

327
00:18:53,300 --> 00:18:55,950
and wether we had a specific
default value, or

328
00:18:55,950 --> 00:18:58,110
we just relied on it returning none.

329
00:18:58,110 --> 00:19:00,850
Then what we have to do is we still have
to check through a ten valley before

330
00:19:00,850 --> 00:19:04,700
we can use the key and
direction to retrieve the next location.

331
00:19:04,700 --> 00:19:10,810
In this case, it makes far more sense to
just use the key using in before using it.

332
00:19:10,810 --> 00:19:14,080
Remember the course that the players
could top anything, not just a letter for

333
00:19:14,080 --> 00:19:15,320
the valuable exits.

334
00:19:15,320 --> 00:19:18,410
Now, the only other comments to make are
that we're displaying the available exists

335
00:19:18,410 --> 00:19:19,220
in an input statement.

336
00:19:19,220 --> 00:19:22,530
And we used upper on the input string, so
that the player could type in upper or

337
00:19:22,530 --> 00:19:23,620
lowercase.

338
00:19:23,620 --> 00:19:25,100
This is the line here on line 24.upper.

339
00:19:25,100 --> 00:19:28,360
There's a slight problem
with this program, though.

340
00:19:28,360 --> 00:19:29,270
Not with the code, but

341
00:19:29,270 --> 00:19:33,570
for us as programmers when we come to add
new locations, and we're using a list

342
00:19:33,570 --> 00:19:37,750
to store the and relying on
the order of the items in the list.

343
00:19:37,750 --> 00:19:42,490
Now that's fine as far as Python goes,
because lists are ordered, When we start

344
00:19:42,490 --> 00:19:46,120
adding 50 or 60 locations and we don't
necessarily add them in numerical order,

345
00:19:46,120 --> 00:19:49,720
we're gonna have to camp down the lines
of code to make sure new entries

346
00:19:49,720 --> 00:19:51,750
are in the correct position
when we build the list.

347
00:19:51,750 --> 00:19:53,820
Now, that will rapidly get tedious.

348
00:19:53,820 --> 00:19:57,260
And as well as that's gonna be a really
excellent source of potential errors.

349
00:19:57,260 --> 00:20:00,330
So it would be much better
if we use the location's key

350
00:20:00,330 --> 00:20:02,690
Also access its exits dictionary.

351
00:20:02,690 --> 00:20:06,250
Now there's another minor problem in
that some players might type west.

352
00:20:06,250 --> 00:20:08,900
They'll put capital W and
a lower case E-S-T instead of just W.

353
00:20:08,900 --> 00:20:10,880
In upper or lowercase.

354
00:20:10,880 --> 00:20:13,210
Now that doesn't matter in this case.

355
00:20:13,210 --> 00:20:17,070
But really what we should do is
allow them to top the full direction

356
00:20:17,070 --> 00:20:18,800
as well as things like go west.

357
00:20:18,800 --> 00:20:21,520
Now we could achieve this by
creating a dictionary containing all

358
00:20:21,520 --> 00:20:24,870
the words players may use as keys with
the values perhaps being the word or

359
00:20:24,870 --> 00:20:26,640
letter that we could understand.

360
00:20:26,640 --> 00:20:28,580
Now that seems like a good idea for
a challenge, so

361
00:20:28,580 --> 00:20:31,270
we're gonna do that challenge and
talk about a challenge to try and

362
00:20:31,270 --> 00:20:32,790
solve that problem in the next video.

363
00:20:32,790 --> 00:20:36,370
And just one other little thing
I type exists instead of exits.

364
00:20:36,370 --> 00:20:37,580
I fixed that up.

365
00:20:37,580 --> 00:20:41,480
And probably what we should do here
also is we should add a space like so.

366
00:20:41,480 --> 00:20:46,100
And that's so when we run this,
there's a space there between

367
00:20:46,100 --> 00:20:49,290
the end of the Available exits, and
where the user can actually start typing.

368
00:20:49,290 --> 00:20:51,100
So anyway,
I'm gonna end this video here now.

369
00:20:51,100 --> 00:20:53,940
In the next video we're gonna tackle
those issues I've talked about

370
00:20:53,940 --> 00:20:54,980
in the form of a challenge.

371
00:20:54,980 --> 00:20:56,220
So I hope to see you in that next video.

