Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,060 --> 00:00:03,150
All right guys, it's time for another code challenge.
2
00:00:03,180 --> 00:00:07,260
So head over to day 4.3 treasure map
3
00:00:07,740 --> 00:00:11,190
and take a look at what is happening here
4
00:00:11,190 --> 00:00:16,190
first. Notice how we've created three lists here for you; row1,
5
00:00:16,590 --> 00:00:20,970
row2, row3, and they've got some blank tiles in here,
6
00:00:21,090 --> 00:00:23,400
and these were just created with emojis.
7
00:00:23,490 --> 00:00:26,910
So if you just search for emoji, um,
8
00:00:27,000 --> 00:00:29,370
and you come across the list of emojis,
9
00:00:29,400 --> 00:00:33,120
then you can simply just copy and paste them right into your code like that.
10
00:00:33,660 --> 00:00:36,090
So depending on whether if you own a Windows or Mac,
11
00:00:36,120 --> 00:00:38,700
you will see the emojis display a little bit differently,
12
00:00:38,910 --> 00:00:41,310
but it doesn't really matter what it looks like.
13
00:00:41,820 --> 00:00:44,940
All that we want to do is to create almost kind of like a,
14
00:00:45,000 --> 00:00:47,670
a chess board or a map like this.
15
00:00:48,450 --> 00:00:51,830
So we have three rows and then we create
16
00:00:51,840 --> 00:00:54,780
another list that includes all three rows.
17
00:00:55,050 --> 00:00:59,460
So this map is actually a nested list because row1 is a list, row2 is a
18
00:00:59,460 --> 00:01:02,610
list, et cetera. And it's all put inside a list.
19
00:01:03,150 --> 00:01:08,150
Then we go ahead and print it so that we add a new line in between each row.
20
00:01:09,720 --> 00:01:13,020
And the final outcome looks something like this.
21
00:01:13,320 --> 00:01:17,610
So if we go ahead and just run the code, you'll see that we have almost like a,
22
00:01:17,610 --> 00:01:22,590
a chess board or essentially a three by three square, right?
23
00:01:23,280 --> 00:01:28,280
And the sort of way that we're going to navigate this map is by using some
24
00:01:29,430 --> 00:01:33,540
indices. So we're going to say that this is column one,
25
00:01:33,570 --> 00:01:37,380
two and three, and this is row one, two and three.
26
00:01:38,430 --> 00:01:42,120
Now, if you've ever played chess, or if you played chess with a computer,
27
00:01:42,540 --> 00:01:47,490
you will know that the different moves or the different positions are often mapped
28
00:01:47,490 --> 00:01:51,630
out using letters and numbers. So for example,
29
00:01:51,720 --> 00:01:56,340
if you want it to move to this square then it would be b4, right?
30
00:01:57,180 --> 00:02:02,180
Or if you want it to move to this square then it would be h3. In our sort
31
00:02:02,550 --> 00:02:03,870
of 3x3 map
32
00:02:04,230 --> 00:02:08,730
the way that we navigate is first by specifying the column,
33
00:02:08,760 --> 00:02:12,810
the horizontal column, and then we specify a number for the row.
34
00:02:13,230 --> 00:02:14,460
So for example,
35
00:02:14,580 --> 00:02:18,480
if I wanted to place my X,
36
00:02:18,540 --> 00:02:23,540
X marks the spot of my treasure at this particular position, then it is one, two,
37
00:02:25,320 --> 00:02:29,550
2 on the horizontal, so column number two, and then one, two,
38
00:02:29,550 --> 00:02:33,870
three, row number three. So I would write that as 23.
39
00:02:34,710 --> 00:02:35,970
And if I wrote 31
40
00:02:35,970 --> 00:02:39,720
then it means I want column three and row one.
41
00:02:39,750 --> 00:02:43,860
So I want you to mark this spot here. So when you run the code,
42
00:02:43,890 --> 00:02:48,270
the idea is that you should be able to specify a location using that two-digit
43
00:02:48,270 --> 00:02:53,250
system, and you should be able to place an X at that location. Now,
44
00:02:53,310 --> 00:02:57,690
in order to do this, it will need you to review what you know about lists,
45
00:02:57,900 --> 00:03:02,900
what you know about using the index to change a particular item in a list
46
00:03:04,480 --> 00:03:06,430
which you saw in previous lessons,
47
00:03:07,090 --> 00:03:09,880
and the added complexity is here
48
00:03:09,880 --> 00:03:14,650
we actually have a nested list. So lists inside lists.
49
00:03:15,100 --> 00:03:20,100
So have a play around with this code and that's the really important word. It's
50
00:03:20,140 --> 00:03:23,950
play, right? There is nothing that could possibly go wrong.
51
00:03:24,370 --> 00:03:26,200
Just try some different lines of code,
52
00:03:26,500 --> 00:03:31,120
try doing some different things and see if it actually prints out the outcome
53
00:03:31,120 --> 00:03:34,810
that you want, where, you know, when you specify a location,
54
00:03:34,870 --> 00:03:37,210
that's the place that gets marked with the capital X.
55
00:03:37,900 --> 00:03:40,540
Pause the video and try to give this challenge a go.
56
00:03:45,570 --> 00:03:47,700
All right. So let's try and solve this.
57
00:03:47,850 --> 00:03:52,170
So notice how at the very bottom of the code, I've got another print statement
58
00:03:52,200 --> 00:03:56,940
which is going to show us the outcome of whatever code it is we write here.
59
00:03:57,480 --> 00:04:02,480
The first thing we probably want to do is get hold of the input position,
60
00:04:02,970 --> 00:04:07,320
right? We know that input always takes in a string.
61
00:04:07,680 --> 00:04:12,090
So even if we typed 23 that's not going to be the number 23,
62
00:04:12,090 --> 00:04:16,290
it's going to be the string with two and three inside the string.
63
00:04:16,680 --> 00:04:18,090
Something that looks like this.
64
00:04:18,780 --> 00:04:23,780
Now we can use what we've learned in previous days and previous lessons to split
65
00:04:24,030 --> 00:04:29,030
that string so that we end up with a horizontal position and a vertical
66
00:04:29,820 --> 00:04:34,200
position. That way we know, well, which column are they interested in
67
00:04:34,260 --> 00:04:38,010
and which row are they interested in. In order to do that,
68
00:04:38,040 --> 00:04:42,990
we tap into this position. And in order to get the horizontal
69
00:04:43,230 --> 00:04:45,540
which we mentioned is the first digit,
70
00:04:45,810 --> 00:04:50,640
then we go ahead and get the zeroth item out of that string.
71
00:04:50,850 --> 00:04:52,710
Remember the string looks like this.
72
00:04:53,010 --> 00:04:55,260
So the zeroth item out of the string is this one.
73
00:04:55,890 --> 00:05:00,540
And then the vertical is going to be the one at position one. Now, of course,
74
00:05:00,600 --> 00:05:03,480
these are still strings as they stand right now.
75
00:05:03,480 --> 00:05:08,480
We've just split up this two-digit string into horizontal and vertical.
76
00:05:09,030 --> 00:05:13,140
So this one, if 23 was the input would be equal to 2,
77
00:05:13,530 --> 00:05:16,920
and this one would be equal to 3.
78
00:05:18,270 --> 00:05:18,570
Now,
79
00:05:18,570 --> 00:05:23,570
once we've gotten hold of the horizontal position and the vertical position,
80
00:05:23,820 --> 00:05:28,380
then we can go ahead and start specifying positions in our list.
81
00:05:28,950 --> 00:05:31,800
So we know that we've got our map, right?
82
00:05:31,830 --> 00:05:34,110
Which is here, this nested list.
83
00:05:34,920 --> 00:05:38,730
And remember that it contains three rows. Now,
84
00:05:38,790 --> 00:05:41,940
if we wanted to get hold of the first row,
85
00:05:42,480 --> 00:05:44,970
then we would write map[0].
86
00:05:45,750 --> 00:05:47,640
If we want to get hold of row2,
87
00:05:47,640 --> 00:05:51,900
then we would write map[1] and map[2] to get row 3.
88
00:05:52,680 --> 00:05:55,740
Now remember that this is the vertical position.
89
00:05:55,740 --> 00:06:00,350
So this is the that we're going to be getting hold of. Or in this example,
90
00:06:00,350 --> 00:06:02,780
it would be the number three, right?
91
00:06:02,780 --> 00:06:06,920
So we could say map passing in the vertical position,
92
00:06:07,670 --> 00:06:09,200
but there's just one problem
93
00:06:09,230 --> 00:06:14,230
and I'll show you if I go ahead and print this map at the position vertical.
94
00:06:16,130 --> 00:06:19,520
Let's put in the example number, which is 23.
95
00:06:19,850 --> 00:06:24,850
And you'll see that the problem is that list indices must be integers,
96
00:06:25,100 --> 00:06:28,250
whole numbers, not a string. Remember that
97
00:06:28,250 --> 00:06:32,690
we said these are still strings. So to convert them into integers,
98
00:06:33,020 --> 00:06:36,170
we use that integer conversion that we've seen before.
99
00:06:36,920 --> 00:06:40,310
So now both the horizontal and vertical are integers,
100
00:06:40,670 --> 00:06:44,830
and we can run this code again and see how it performs.
101
00:06:47,800 --> 00:06:51,100
Let's put 23 in there, and
102
00:06:51,160 --> 00:06:53,950
we ended up with the error that we learned about in the last lesson
103
00:06:54,310 --> 00:06:59,200
which is an index error. So it's out of range. So what's going on here?
104
00:06:59,770 --> 00:07:04,750
Remember that in our example where the number is 23, well
105
00:07:04,750 --> 00:07:08,440
then the horizontal is the first number. So it's going to be 2.
106
00:07:09,070 --> 00:07:11,740
And then the vertical is going to be the second number,
107
00:07:11,740 --> 00:07:13,240
so it's going to be 3.
108
00:07:13,810 --> 00:07:18,040
And then we try to get a hold of that particular row from our map.
109
00:07:18,700 --> 00:07:21,850
Now, if we pass 3 into our map,
110
00:07:22,090 --> 00:07:26,710
then you can clearly see what's going to happen. There's 0, 1, 2,
111
00:07:26,770 --> 00:07:28,810
there is nothing at position three.
112
00:07:29,230 --> 00:07:33,250
There is nothing that has been offset from the beginning by three, right?
113
00:07:34,060 --> 00:07:35,500
So that doesn't exist.
114
00:07:35,890 --> 00:07:39,340
And this is why we get our index out of range error.
115
00:07:39,880 --> 00:07:41,770
So what do we have to do instead? Well,
116
00:07:41,770 --> 00:07:46,480
we have to remove one from it to shift it to row three,
117
00:07:46,510 --> 00:07:51,040
because that's really what we're interested in when we're writing this here, right?
118
00:07:51,700 --> 00:07:55,480
We can do the same by substituting the three with vertical
119
00:07:55,750 --> 00:08:00,280
so it'll work with any number. And now if I run my code again, type
120
00:08:00,280 --> 00:08:01,113
23,
121
00:08:01,150 --> 00:08:06,150
then you can see that I get no more errors and it actually prints out a row that
122
00:08:06,430 --> 00:08:09,040
I've selected, which is row three.
123
00:08:09,250 --> 00:08:14,050
Now you can of course change these just to make it a little bit more clear to
124
00:08:14,050 --> 00:08:18,550
yourself what's actually going on here. Let's type 23 again,
125
00:08:18,850 --> 00:08:20,980
and you can see that this is what's being printed.
126
00:08:22,390 --> 00:08:26,770
Now the next step is once I've gotten hold of row 3,
127
00:08:27,250 --> 00:08:30,940
how can I get the horizontal tile?
128
00:08:31,420 --> 00:08:34,780
So the horizontal tile we're interested in is the second one.
129
00:08:34,810 --> 00:08:39,250
So it's actually this one. So when we write
130
00:08:39,250 --> 00:08:42,940
23, then it's this tile that we're interested in.
131
00:08:43,960 --> 00:08:47,080
So instead of printing out this row 3
132
00:08:47,110 --> 00:08:50,380
which we selected by writing this code,
133
00:08:51,010 --> 00:08:56,010
we can save this as the selected_row. And then inside the selected_row,
134
00:08:58,080 --> 00:09:01,890
we can get to the tile at the horizontal position that was specified.
135
00:09:02,400 --> 00:09:07,400
So we could say selected row at the position of the horizontal - 1.
136
00:09:09,960 --> 00:09:14,910
This works the same way because our numbers that we're entering here,
137
00:09:14,910 --> 00:09:19,470
row one, two, three, or column one, two, three, they don't start from zero.
138
00:09:19,620 --> 00:09:23,730
So we have to shift it down by one in order for it to work.
139
00:09:24,300 --> 00:09:28,770
So, now once we've gotten hold of the selected row using the vertical,
140
00:09:29,280 --> 00:09:32,820
and then the selected tile using the horizontal, well,
141
00:09:32,820 --> 00:09:34,320
now we can actually change it.
142
00:09:34,710 --> 00:09:38,520
And the thing we want to change it to is a capital X.
143
00:09:39,210 --> 00:09:42,540
So now if I go ahead and run my code,
144
00:09:42,870 --> 00:09:47,370
then you can see it works just as the instructions want it to work.
145
00:09:47,760 --> 00:09:51,570
So here's our map. Where do you wanna put the treasure? If I write
146
00:09:51,570 --> 00:09:55,830
23, then it will put the treasure on column number two, row
147
00:09:55,860 --> 00:09:59,880
number three. Now, if I run this code again and I say, well,
148
00:09:59,880 --> 00:10:03,810
now I want to put it in column number one, row number three,
149
00:10:04,140 --> 00:10:05,190
then there it is.
150
00:10:05,520 --> 00:10:10,170
So this is now working for any possible combination that we come up with
151
00:10:10,380 --> 00:10:13,380
as long as we're using the coordinates of our map.
152
00:10:14,400 --> 00:10:18,630
Did you manage to get it right? And did you write the code in a different way,
153
00:10:18,630 --> 00:10:23,190
perhaps? Because there's many, many ways of doing the same thing.
154
00:10:23,760 --> 00:10:28,290
And the easiest thing that I can spot right now is what if we didn't need to
155
00:10:28,290 --> 00:10:33,290
separate this into two lines? Instead of creating a selected row,
156
00:10:33,960 --> 00:10:37,350
we could simply just tag on this at the end,
157
00:10:38,310 --> 00:10:41,400
get a hold of this particular row from the map,
158
00:10:41,700 --> 00:10:44,970
and because this is going to turn into a list,
159
00:10:46,020 --> 00:10:48,660
namely one of these, well,
160
00:10:48,660 --> 00:10:53,660
then we can simply just use another square bracket to specify the column.
161
00:10:55,200 --> 00:10:59,190
And then we set that tile to a capital X.
162
00:10:59,580 --> 00:11:03,450
So depending on which format you find it easier to understand,
163
00:11:03,730 --> 00:11:06,960
you can keep your code either like this or like this.
164
00:11:07,260 --> 00:11:09,060
This is obviously a lot more succinct,
165
00:11:09,120 --> 00:11:12,030
but it depends on whether if this logic actually makes sense to you.
166
00:11:12,840 --> 00:11:14,550
So have a think about what happened here,
167
00:11:14,790 --> 00:11:18,840
and if you didn't manage to complete the exercise, then try to give it another go.
168
00:11:18,840 --> 00:11:20,880
Now in the next lesson,
169
00:11:21,150 --> 00:11:24,780
we're going to tackle our final project of the day.
170
00:11:25,140 --> 00:11:28,680
So once you're prepared and once you're happy that you have understood
171
00:11:28,680 --> 00:11:30,840
everything that we've covered so far,
172
00:11:31,170 --> 00:11:34,920
then head over to the next lesson where we're going to get started building our
173
00:11:34,920 --> 00:11:36,510
rock-paper-scissors game.
16805
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.