So now it's time to move on to Step 2 in the five steps to create our hangman game.
Now in Step 2, I recommend reading through the Description for a more detailed breakdown of each
TODO, because I don't want to clutter the code file with a bunch of text, but also I need to be able
to put the TODOs in so that I can tell you where you need to write each of the blocks of code.
So take a look through the TODOs and any hints if necessary.
Ideally, if you can create the code for the solution without looking at the hints, that would be great,
but the hints are there for a purpose.
So if you get stuck, check them out.
Now, as always, we're going to start by running the solution code without viewing the solution.
And you can see what we're trying to achieve in this step is we've got the chosen_word printed.
We want to print out a set of blanks, because when we actually have the final game, we're not going
to show the user the solution.
Instead, we're going to show them, "This is how many letters you need to guess."
And we're showing it to them in these blanks, which are basically created with these underscore symbols.
Now the next step is the user will go ahead and guess a letter.
And what we want to do in TODO Number 2 is to create a variable called display that is going to put
the guessed letter into the right position.
So you can see an aardvark.
We've got two A's at the beginning and then an A at this position as well.
So we're creating a string where the user-guessed letter is put into the right positions of the word,
but the unguessed letters are represented by underscores, which will represent a blank for the user
to continue guessing.
So as we're on Step 2, it's getting harder, but hopefully, you'll be able to figure out how to do
this by looking at the TODOs, the hints, and also using your knowledge of Python.
So I'm going to let you pause the video now and try to tackle this challenge.
Okay, so the first thing we need to do is TODO Number 1.
And we're going to write our code right here underneath it we're going to create an empty string called,
"placeholder".
So that's quite easy.
And we're going to set it to equal an empty string with the double quotes with nothing in between them.
Next we need to create a for loop so that for each letter in the chosen_word, we're going to add a
underscore symbol to this placeholder.
So for example, if the chosen word was apple then the placeholder will just be five underscores.
So how can we do that.
Well we learned about the for in range() function before.
So we could for example say for and at this point you can name this variable whatever you like, but
I'm going to call it, "position," for each of the positions in the range...
Now what is the range that we want.
Well, if the word was apple then we would want that range to be the number 6, because we want this
loop to run five times.
And we know that range goes from the starting point to the end point.
So if we wanted to write (1, 6), then go ahead and print,
"This loop has run". And I go ahead and run this current file.
You'll see it prints us out one, two, three, four, five times,
which means that instead of printing, "This loop has run", I could assign the placeholder a new value.
So the += will add a new letter to that string.
And I could add the underscore so that each time the loop runs we add an underscore to this string placeholder.
So let's go ahead and print(placeholder).
And let's rerun this current file.
And you can see here we've got our string placeholder being printed out,
and it's composed of five underscores because our range ran five times between one up to but not including
six.
So if we want the range to instead run to the number of characters in our chosen_word, then all we
need to do is to create a word_length variable and we can set it to equal the length of the chosen_word.
And now if we put that in here as the range and go ahead and rerun this, then you can see that we're
getting the same number of times that loop runs as the number of characters in the word.
So now we're basically creating this placeholder so that the user can look at this instead of the actual
answer and try to guess the letter.
Now, if this was at all confusing, do go back and revise the lesson where we learned about the range()
function in the context of for loops in Python, it hopefully will make a bit more sense, and then
you can come back here and fix your code.
Now the next TODO is to create a variable called display that puts the guessed letter into the right
positions and a blank in the rest of the string.
So for example, in the Description here, you can see that if the word was Apple and the user
guessed "p", then this display should put the "p" letters in the right positions, and then the rest of
the positions have a blank.
So we're going to reuse or rather extend this for loop that we created previously.
So we know that we can loop through each of the letters in the chosen_word,
and if the letter matches the guessed letter then we'll print, "Right",
otherwise we'll print wrong.
But instead of printing "Right" and "Wrong", what we want to do instead is to create a new variable called
display and set this to equal an empty string.
Now if in our loop, we check the current letter and it's equal to what the user guessed in their input,
then we're going to take this display and we're going to use the += to add the current letter
that we're looping through to this display.
And if the letter is not a match then we're going to take the display and we're going to give it an
underscore instead.
So now if I go ahead and finally print out the final value of display at the end of the running of this
loop, then we can see it in action.
So the current word that's being chosen is aardvark.
This is the placeholder being printed out with the number of letters in that word.
And then we're asked to guess a letter.
So I'm going to guess A.
And then you can see that for each time this loop runs, we'll loop through the first letter in the chosen_word
,
A, is it equal to the guessed letter?
Well it is.
Well, in that case, we're going to add that letter to the display.
And that's how we have this first a.
Now when we loop to this R, so this letter is equal to R, "Is the letter equal to guess? No."
So it's going to trigger the else.
And in this case it just adds an underscore to that string display instead.
And by the end of this entire process we end up with the display showing the letters which were correctly
guessed, and blanks for the letters which were not.
So that's all there is to it, to the two TODOs in Step 2.
We're now creating the basis of our game that we're going to build on in future steps. So hopefully
that makes sense,
if it didn't,
as always, go and fix your code and make sure you understand what's going on before you proceed.
Otherwise I'll see you in the next lesson.