All right, let's go ahead and run through the solution together. Firstly,
I'm going to fork my own copy of the starting file for step two
and then I'm going to take a look at to-do number one.
So the goal is to create an empty list called display,
and for each letter in the chosen word,
remember that a random word from this list,
we're going to add an underscore to this list called display.
So it tells us that if the word was apple,
then display should contain five underscores representing each letter the
user needs to guess.
Let's go ahead and just click run before doing anything first.
And this print statement will execute and it'll tell us that the solution is
ardvark. This is a seven letter word. So in this case,
we would need to create a list with seven underscores and seven items.
Let's go ahead and create an empty list called display.
An empty list is just a set of square brackets with nothing inside.
Now, for each letter in the chosen word,
we're going to add a underscore to this list.
How can we add something for each letter?
The simplest way is to simply use a for-loop. Now, in this case,
we have a choice.
We can either use the style of for-loop that we used previously down here,
which is a way of looping through a list.
So we could say for letter in chosen_word,
so it's going to run this for as many letters as there are in this word.
And for every single letter,
we're just simply going to take our list, display, and add a item to it.
And that item is going to be the string, underscore.
So now if we go ahead and print out our display and see what we get.
The solution is baboon
which has six letters and display gets printed with one, two, three, four,
five, six underscores inside.
Now notice how here we're not actually using this variable letter that gets
created. We're looping through every letter in the chosen word,
but we're not doing anything with each of the letters. So we can in fact
just replace it with an underscore. Now, alternatively,
we could have created this for loop with a range function as well.
And remember that the range function creates a range of numbers from zero up to
a particular number that you specify. So we could have also,
instead of looping through the chosen word, use range and inside here,
we could have put the length of the chosen word.
So then our range would be between zero and the length of the chosen word.
So it was a baboon then it would be from zero to six, but not including six.
Remember that's how the range operator works.
So it would be zero and then it would be one and so on and so forth until it
gets to five.
And so that would be a total of six loops adding six of these underscores.
So given that I've updated this for loop, if we run it again,
you can see its exactly the same,
it will create as many underscores in the list as there are letters in the
chosen word. So it doesn't actually matter in this case
which version you went for, either this version of the
for loop using the range function or the previous one
which just looped through the list. It's your choice. They both worked the same.
So now let's move on to to-do number two.
Now we have to loop through each position in the chosen word
and if the letter at that position matches the letter the user guessed,
then we're going to reveal that letter in the display at that position.
So basically we're going to replace the underscore with the letter that the user
guessed if they guessed it right.
So now this is probably the hardest part of this whole step because in order to
do this,
we actually have to change this for loop to
one that uses the range function because we need to get hold of the position
when the letter and the guess actually matched. That way
we can tap into the list and change only the item at that particular position.
So let me show you how the code would look.
Instead of saying for letter in chosen_word,
we would say for position in range
and the range is going to be between zero and the length of the chosen_word.
And once we've got that, then the
for-loop is going to run for as many times as there are letters in the word and
for each of those letters
it's going to give us a number, a position to work with.
So the first time it runs it position is going to be equal to zero because we'll
be looking at the first item in the chosen_word,
which in this case would be a.
And then the next time the loop runs position becomes one
and we'll be looking at the second item in the list.
Now we no longer have that letter to work with,
which is why we get an error here.
So how can we get hold of the current letter in the chosen word
given that we know the position? Well,
it would be as simple as saying letter = chosen_word
[position].
Now we can check to see if the letter is equal to the letter
the user guessed.
Then we can get hold of our display and get the item at the current position and
then set that to equal the letter.
And we can get rid of the else statement entirely.
So now notice that we're using the length of the chosen word in quite a few
places. So if you want to,
you can actually simplify the code by simply creating a new variable called word
length.
And now every time we need to use the word_length,
we can just refer to that variable
Right?
which should make our code a little bit easier to read.
Right?
Finally, all we have to do for step three is just to print our list
which is called display. And then when we run our code,
you should be able to see that if the solution was camel,
initially, our display starts out with five blanks because there's five letters.
And then once we've guessed a letter,
then it will replace that letter at the correct position in that display list.
So a only matches the second letter,
so that second blank gets replaced by this letter.
And again,
I recommend to review the code if it's at all confusing or if you didn't manage
to get it first time through the Thonny IDE just to see it step by step and see
what's actually happening. We've chosen our random word,
which in this case turns out to be camel.
And once we get into our display,
it starts out as an empty list.
But as soon as we calculate the word length,
then we can go ahead and loop through the range of numbers from zero to the
length of the word, which is five. And it will run the code in the
for loop
which is just to add the underscore to this list five times.
And finally, once it's done, it's going to print it out.
So this is what we get, five blanks for the word camel.
Now it asked the user for a guess.
So I'm going to guess that letter a and enters this final for loop.
This is where it's really interesting.
So we're going to step into it to see it in more detail.
So now the range is a range between zero and the word length.
So the word length is five
so that range becomes from zero to five not
including five. So zero, one, two, three, four.
And that is going to make the for loop run five times.
The position starts out with zero. And when position is zero
then it's going to go into this next line, line 18, and put zero here as the
position. And then we go into the chosen word,
which is camel and pick out the item at position zero.
And so this letter becomes equal to the letter c because that's the letter at
position zero of the word. So now in our if statement,
we check to see if the letter, which is c, is equal to the guess,
which is a. Well in this case, c does not equal a
so we go back to the beginning of the for loop and we go to the next number in
the range, which is now one.
And we're going to repeat this again and again. But in this case,
notice that the letter at position one of camel is equal to a.
So now letter is equal to a and guess is equal to a.
So if those two are equal, then this
if statement is true and we end up going inside it to carry out this
functionality. Our letter is equal to a,
and we're going to look at our list, which is called display, this one,
and we're going to replace that second character with the letter that was
matching. And it's going to do this again and again
until we've looped through five times through all of the letters in the chosen
word
and finally we print the final version of display with the correct letters
replaced. Have a play around with this
if you're at all confused and it might be that you need to look up some stuff on
Stack Overflow,
or you might need to review some of the lessons that we've done before on things
like range functions and for loops, just so that you're really,
really certain that you know
what's going on and only then should you continue to the next step.
Take a moment with this and I'll see you on the next lesson.