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
All right guys, it's time for another code challenge.
So head over to day 4.3 treasure map
and take a look at what is happening here
first. Notice how we've created three lists here for you; row1,
row2, row3, and they've got some blank tiles in here,
and these were just created with emojis.
So if you just search for emoji, um,
and you come across the list of emojis,
then you can simply just copy and paste them right into your code like that.
So depending on whether if you own a Windows or Mac,
you will see the emojis display a little bit differently,
but it doesn't really matter what it looks like.
All that we want to do is to create almost kind of like a,
a chess board or a map like this.
So we have three rows and then we create
another list that includes all three rows.
So this map is actually a nested list because row1 is a list, row2 is a
list, et cetera. And it's all put inside a list.
Then we go ahead and print it so that we add a new line in between each row.
And the final outcome looks something like this.
So if we go ahead and just run the code, you'll see that we have almost like a,
a chess board or essentially a three by three square, right?
And the sort of way that we're going to navigate this map is by using some
indices. So we're going to say that this is column one,
two and three, and this is row one, two and three.
Now, if you've ever played chess, or if you played chess with a computer,
you will know that the different moves or the different positions are often mapped
out using letters and numbers. So for example,
if you want it to move to this square then it would be b4, right?
Or if you want it to move to this square then it would be h3. In our sort
of 3x3 map
the way that we navigate is first by specifying the column,
the horizontal column, and then we specify a number for the row.
So for example,
if I wanted to place my X,
X marks the spot of my treasure at this particular position, then it is one, two,
2 on the horizontal, so column number two, and then one, two,
three, row number three. So I would write that as 23.
And if I wrote 31
then it means I want column three and row one.
So I want you to mark this spot here. So when you run the code,
the idea is that you should be able to specify a location using that two-digit
system, and you should be able to place an X at that location. Now,
in order to do this, it will need you to review what you know about lists,
what you know about using the index to change a particular item in a list
which you saw in previous lessons,
and the added complexity is here
we actually have a nested list. So lists inside lists.
So have a play around with this code and that's the really important word. It's
play, right? There is nothing that could possibly go wrong.
Just try some different lines of code,
try doing some different things and see if it actually prints out the outcome
that you want, where, you know, when you specify a location,
that's the place that gets marked with the capital X.
Pause the video and try to give this challenge a go.
All right. So let's try and solve this.
So notice how at the very bottom of the code, I've got another print statement
which is going to show us the outcome of whatever code it is we write here.
The first thing we probably want to do is get hold of the input position,
right? We know that input always takes in a string.
So even if we typed 23 that's not going to be the number 23,
it's going to be the string with two and three inside the string.
Something that looks like this.
Now we can use what we've learned in previous days and previous lessons to split
that string so that we end up with a horizontal position and a vertical
position. That way we know, well, which column are they interested in
and which row are they interested in. In order to do that,
we tap into this position. And in order to get the horizontal
which we mentioned is the first digit,
then we go ahead and get the zeroth item out of that string.
Remember the string looks like this.
So the zeroth item out of the string is this one.
And then the vertical is going to be the one at position one. Now, of course,
these are still strings as they stand right now.
We've just split up this two-digit string into horizontal and vertical.
So this one, if 23 was the input would be equal to 2,
and this one would be equal to 3.
Now,
once we've gotten hold of the horizontal position and the vertical position,
then we can go ahead and start specifying positions in our list.
So we know that we've got our map, right?
Which is here, this nested list.
And remember that it contains three rows. Now,
if we wanted to get hold of the first row,
then we would write map[0].
If we want to get hold of row2,
then we would write map[1] and map[2] to get row 3.
Now remember that this is the vertical position.
So this is the that we're going to be getting hold of. Or in this example,
it would be the number three, right?
So we could say map passing in the vertical position,
but there's just one problem
and I'll show you if I go ahead and print this map at the position vertical.
Let's put in the example number, which is 23.
And you'll see that the problem is that list indices must be integers,
whole numbers, not a string. Remember that
we said these are still strings. So to convert them into integers,
we use that integer conversion that we've seen before.
So now both the horizontal and vertical are integers,
and we can run this code again and see how it performs.
Let's put 23 in there, and
we ended up with the error that we learned about in the last lesson
which is an index error. So it's out of range. So what's going on here?
Remember that in our example where the number is 23, well
then the horizontal is the first number. So it's going to be 2.
And then the vertical is going to be the second number,
so it's going to be 3.
And then we try to get a hold of that particular row from our map.
Now, if we pass 3 into our map,
then you can clearly see what's going to happen. There's 0, 1, 2,
there is nothing at position three.
There is nothing that has been offset from the beginning by three, right?
So that doesn't exist.
And this is why we get our index out of range error.
So what do we have to do instead? Well,
we have to remove one from it to shift it to row three,
because that's really what we're interested in when we're writing this here, right?
We can do the same by substituting the three with vertical
so it'll work with any number. And now if I run my code again, type
23,
then you can see that I get no more errors and it actually prints out a row that
I've selected, which is row three.
Now you can of course change these just to make it a little bit more clear to
yourself what's actually going on here. Let's type 23 again,
and you can see that this is what's being printed.
Now the next step is once I've gotten hold of row 3,
how can I get the horizontal tile?
So the horizontal tile we're interested in is the second one.
So it's actually this one. So when we write
23, then it's this tile that we're interested in.
So instead of printing out this row 3
which we selected by writing this code,
we can save this as the selected_row. And then inside the selected_row,
we can get to the tile at the horizontal position that was specified.
So we could say selected row at the position of the horizontal - 1.
This works the same way because our numbers that we're entering here,
row one, two, three, or column one, two, three, they don't start from zero.
So we have to shift it down by one in order for it to work.
So, now once we've gotten hold of the selected row using the vertical,
and then the selected tile using the horizontal, well,
now we can actually change it.
And the thing we want to change it to is a capital X.
So now if I go ahead and run my code,
then you can see it works just as the instructions want it to work.
So here's our map. Where do you wanna put the treasure? If I write
23, then it will put the treasure on column number two, row
number three. Now, if I run this code again and I say, well,
now I want to put it in column number one, row number three,
then there it is.
So this is now working for any possible combination that we come up with
as long as we're using the coordinates of our map.
Did you manage to get it right? And did you write the code in a different way,
perhaps? Because there's many, many ways of doing the same thing.
And the easiest thing that I can spot right now is what if we didn't need to
separate this into two lines? Instead of creating a selected row,
we could simply just tag on this at the end,
get a hold of this particular row from the map,
and because this is going to turn into a list,
namely one of these, well,
then we can simply just use another square bracket to specify the column.
And then we set that tile to a capital X.
So depending on which format you find it easier to understand,
you can keep your code either like this or like this.
This is obviously a lot more succinct,
but it depends on whether if this logic actually makes sense to you.
So have a think about what happened here,
and if you didn't manage to complete the exercise, then try to give it another go.
Now in the next lesson,
we're going to tackle our final project of the day.
So once you're prepared and once you're happy that you have understood
everything that we've covered so far,
then head over to the next lesson where we're going to get started building our
rock-paper-scissors game.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.