So now we're inside Step 4.
So we're almost there at the end now closing in on the finish line.
So there are three TODOs in this step,
and you'll notice that when you look at the starting file I've got the different stages of hangman as
ASCII art for us to be able to use.
And they go from 6,
so which is the start of the hangman
with no man, all the way down to 0, where the art is complete,
and that's when the user would be in Game Over status.
And we're going to combine that, among other things, into our game.
So let's first take a look at a run of the solution.
So this is what we're aiming to achieve by the end of this step.
So we want to be able to guess a letter,
and you'll notice that when you guess a correct letter, then the hangman art does not progress.
So if the word is baboon I guess "b", then there is firstly art with no man.
And then if I guess another correct letter, you'll see the art doesn't change,
it's still just the rack.
Now if I guess an incorrect letter, let's try F,
you'll see that the hangman picture starts appearing.
And if I keep guessing incorrect letters, you'll see that the person is slowly being drawn until we
get to the very, very end where we end up with the complete image, which is image [0] in our list
called stages.
And at this point we get the message, "You lose."
So that's all that we're trying to achieve in this particular step.
And if you take a look in the TODOs, either in the description, now that this is updated, or
in the main.py, you should be able to see the individual steps which you have to go through.
So TODO Number 1, is to create a variable called lives to keep track of how many lives the user has
left.
They start out with 6, and once that reaches 0, then the game should stop and it should print,
"You lose."
So how does it go down to 0?
Well, every time the user guesses a letter that is not in the chosen word, then we have to reduce
their "lives" by one until eventually at some point it'll reach zero and they lose.
Now, finally, we're going to incorporate the ASCII art from this list, which is called stages that
I've added in the starting file for you.
And you're going to print it out so that it corresponds to the current number of lives the user has
remaining.
And you might want to print out the stages using different indices or different positions to
pick out different pictures and just get a feel for how they're ordered.
So that's all there is to it.
And if you're ready, then go ahead and pause the video now and attempt Step 4.
All right.
So let's start from TODO Number 1.
This is probably the easiest TODO out of this whole project,
create a variable called lives and set it equal to 6.
That's it, TODO Number 1 done.
Next,
if the guess, so the current letter that the user has guessed based on their input is not a letter in
the chosen_word, so it doesn't exist in the chosen_word, then we need to reduce lives by 1.
Now you might remember that we have this if, elif, else statement here that checks to see if the guess
is equal to the letter in the chosen_word.
So by looping through each of the letters and one of them matches the guess, then this section does
correspond to when the letter is in the chosen_word, but it does not correspond to when the letter
is not in the chosen_word.
So the situation where the user has guessed the letter that does not exist in the word.
So rather than targeting the elif and else, or messing with this if statement, we can actually simply
just write a completely separate statement so that we keep our logic separate and under different banners,
and it will probably be easier to debug as well.
So we can use the "not in" keyword.
So we can say if the guess is not in the chosen_word,
then under this condition, do we reduce the number of lives -= 1.
And then if at this point, after we've subtracted 1 from lives, if lives is now equal to 0 at
this point, well then that's game over, right?
So we set game_over to True.
And that means we break out of our while loop.
And in addition, we're also going to tell the user that this has happened by telling them, "You lose."
So that's TODO Number 2 tackled.
Now the final TODO is simply just to print the ASCII art from this list corresponding to the current
number of lives.
Now, very helpfully, I've ordered the art in this list so that they correspond to the number of lives,
so it starts out at 0,
of course. This is what the picture should display when the user has zero lives.
And also it goes all the way up to six, so that when the user has 6 lives, full lives, then you don't
see the man at all.
So that means it's as simple as simply getting the right indentation,
so being inside our while loop, because we want to print these ASCII art each time the user makes a
guess.
And then we simply use the print statement to tap into that list, and then use the square brackets
to choose the correct index or the correct position for the art we want to display.
And as I just mentioned, it's matched to our lives situation.
So that's all that we need to do in order to complete this, to do.
And now if you run this code, you'll see that as long as I keep getting the right letter, my art remains
on this particular picture, which is corresponding to lives equals six.
And then as I make incorrect guesses, then my hangman picture starts progressing all the way until
I end up with no lives,
which is the last picture here.
Lives equals six, and I get the, "You lose" printed out.
So did you manage to solve these steps?
I would say this particular step is probably a little bit easier than some of the previous ones, but
as you've noticed, the difficulty can vary in this project and I just want to guide you from step to
step, making sure that each to do and each of the step codes actually make sense.
So if there's something you realize about previous steps that doesn't make sense, go back and rewatch
it and just make sure that all the code that you see at this stage makes perfect sense to you, and
that you would be able to write it from scratch.