Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
All right guys, it's time for another code challenge.
2
So head over to day 4.3 treasure map
3
and take a look at what is happening here
4
first. Notice how we've created three lists here for you; row1,
5
row2, row3, and they've got some blank tiles in here,
6
and these were just created with emojis.
7
So if you just search for emoji, um,
8
and you come across the list of emojis,
9
then you can simply just copy and paste them right into your code like that.
10
So depending on whether if you own a Windows or Mac,
11
you will see the emojis display a little bit differently,
12
but it doesn't really matter what it looks like.
13
All that we want to do is to create almost kind of like a,
14
a chess board or a map like this.
15
So we have three rows and then we create
16
another list that includes all three rows.
17
So this map is actually a nested list because row1 is a list, row2 is a
18
list, et cetera. And it's all put inside a list.
19
Then we go ahead and print it so that we add a new line in between each row.
20
And the final outcome looks something like this.
21
So if we go ahead and just run the code, you'll see that we have almost like a,
22
a chess board or essentially a three by three square, right?
23
And the sort of way that we're going to navigate this map is by using some
24
indices. So we're going to say that this is column one,
25
two and three, and this is row one, two and three.
26
Now, if you've ever played chess, or if you played chess with a computer,
27
you will know that the different moves or the different positions are often mapped
28
out using letters and numbers. So for example,
29
if you want it to move to this square then it would be b4, right?
30
Or if you want it to move to this square then it would be h3. In our sort
31
of 3x3 map
32
the way that we navigate is first by specifying the column,
33
the horizontal column, and then we specify a number for the row.
34
So for example,
35
if I wanted to place my X,
36
X marks the spot of my treasure at this particular position, then it is one, two,
37
2 on the horizontal, so column number two, and then one, two,
38
three, row number three. So I would write that as 23.
39
And if I wrote 31
40
then it means I want column three and row one.
41
So I want you to mark this spot here. So when you run the code,
42
the idea is that you should be able to specify a location using that two-digit
43
system, and you should be able to place an X at that location. Now,
44
in order to do this, it will need you to review what you know about lists,
45
what you know about using the index to change a particular item in a list
46
which you saw in previous lessons,
47
and the added complexity is here
48
we actually have a nested list. So lists inside lists.
49
So have a play around with this code and that's the really important word. It's
50
play, right? There is nothing that could possibly go wrong.
51
Just try some different lines of code,
52
try doing some different things and see if it actually prints out the outcome
53
that you want, where, you know, when you specify a location,
54
that's the place that gets marked with the capital X.
55
Pause the video and try to give this challenge a go.
56
All right. So let's try and solve this.
57
So notice how at the very bottom of the code, I've got another print statement
58
which is going to show us the outcome of whatever code it is we write here.
59
The first thing we probably want to do is get hold of the input position,
60
right? We know that input always takes in a string.
61
So even if we typed 23 that's not going to be the number 23,
62
it's going to be the string with two and three inside the string.
63
Something that looks like this.
64
Now we can use what we've learned in previous days and previous lessons to split
65
that string so that we end up with a horizontal position and a vertical
66
position. That way we know, well, which column are they interested in
67
and which row are they interested in. In order to do that,
68
we tap into this position. And in order to get the horizontal
69
which we mentioned is the first digit,
70
then we go ahead and get the zeroth item out of that string.
71
Remember the string looks like this.
72
So the zeroth item out of the string is this one.
73
And then the vertical is going to be the one at position one. Now, of course,
74
these are still strings as they stand right now.
75
We've just split up this two-digit string into horizontal and vertical.
76
So this one, if 23 was the input would be equal to 2,
77
and this one would be equal to 3.
78
Now,
79
once we've gotten hold of the horizontal position and the vertical position,
80
then we can go ahead and start specifying positions in our list.
81
So we know that we've got our map, right?
82
Which is here, this nested list.
83
And remember that it contains three rows. Now,
84
if we wanted to get hold of the first row,
85
then we would write map[0].
86
If we want to get hold of row2,
87
then we would write map[1] and map[2] to get row 3.
88
Now remember that this is the vertical position.
89
So this is the that we're going to be getting hold of. Or in this example,
90
it would be the number three, right?
91
So we could say map passing in the vertical position,
92
but there's just one problem
93
and I'll show you if I go ahead and print this map at the position vertical.
94
Let's put in the example number, which is 23.
95
And you'll see that the problem is that list indices must be integers,
96
whole numbers, not a string. Remember that
97
we said these are still strings. So to convert them into integers,
98
we use that integer conversion that we've seen before.
99
So now both the horizontal and vertical are integers,
100
and we can run this code again and see how it performs.
101
Let's put 23 in there, and
102
we ended up with the error that we learned about in the last lesson
103
which is an index error. So it's out of range. So what's going on here?
104
Remember that in our example where the number is 23, well
105
then the horizontal is the first number. So it's going to be 2.
106
And then the vertical is going to be the second number,
107
so it's going to be 3.
108
And then we try to get a hold of that particular row from our map.
109
Now, if we pass 3 into our map,
110
then you can clearly see what's going to happen. There's 0, 1, 2,
111
there is nothing at position three.
112
There is nothing that has been offset from the beginning by three, right?
113
So that doesn't exist.
114
And this is why we get our index out of range error.
115
So what do we have to do instead? Well,
116
we have to remove one from it to shift it to row three,
117
because that's really what we're interested in when we're writing this here, right?
118
We can do the same by substituting the three with vertical
119
so it'll work with any number. And now if I run my code again, type
120
23,
121
then you can see that I get no more errors and it actually prints out a row that
122
I've selected, which is row three.
123
Now you can of course change these just to make it a little bit more clear to
124
yourself what's actually going on here. Let's type 23 again,
125
and you can see that this is what's being printed.
126
Now the next step is once I've gotten hold of row 3,
127
how can I get the horizontal tile?
128
So the horizontal tile we're interested in is the second one.
129
So it's actually this one. So when we write
130
23, then it's this tile that we're interested in.
131
So instead of printing out this row 3
132
which we selected by writing this code,
133
we can save this as the selected_row. And then inside the selected_row,
134
we can get to the tile at the horizontal position that was specified.
135
So we could say selected row at the position of the horizontal - 1.
136
This works the same way because our numbers that we're entering here,
137
row one, two, three, or column one, two, three, they don't start from zero.
138
So we have to shift it down by one in order for it to work.
139
So, now once we've gotten hold of the selected row using the vertical,
140
and then the selected tile using the horizontal, well,
141
now we can actually change it.
142
And the thing we want to change it to is a capital X.
143
So now if I go ahead and run my code,
144
then you can see it works just as the instructions want it to work.
145
So here's our map. Where do you wanna put the treasure? If I write
146
23, then it will put the treasure on column number two, row
147
number three. Now, if I run this code again and I say, well,
148
now I want to put it in column number one, row number three,
149
then there it is.
150
So this is now working for any possible combination that we come up with
151
as long as we're using the coordinates of our map.
152
Did you manage to get it right? And did you write the code in a different way,
153
perhaps? Because there's many, many ways of doing the same thing.
154
And the easiest thing that I can spot right now is what if we didn't need to
155
separate this into two lines? Instead of creating a selected row,
156
we could simply just tag on this at the end,
157
get a hold of this particular row from the map,
158
and because this is going to turn into a list,
159
namely one of these, well,
160
then we can simply just use another square bracket to specify the column.
161
And then we set that tile to a capital X.
162
So depending on which format you find it easier to understand,
163
you can keep your code either like this or like this.
164
This is obviously a lot more succinct,
165
but it depends on whether if this logic actually makes sense to you.
166
So have a think about what happened here,
167
and if you didn't manage to complete the exercise, then try to give it another go.
168
Now in the next lesson,
169
we're going to tackle our final project of the day.
170
So once you're prepared and once you're happy that you have understood
171
everything that we've covered so far,
172
then head over to the next lesson where we're going to get started building our
173
rock-paper-scissors game.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.