All right. So make sure you've given it a good go before you come over here,
because I'm about to reveal the solution. In terms of step 4, here
I've got a forked copy of the code. And if I scroll down to
to-do number one, this is pretty simple.
It just tells us to create a new variable called lives
and this variable is going to be the one that's going to keep track of the
number of lives the user has left. And to start off,
it's going to be equal to six. Now we move on to to-do number two.
And it tells us that if the guess is not a letter in the chosen word,
then we have to reduce the number of lives by one.
Now it might be really tempting to put this as an else statement here,
because if the letter is equal to guess,
then well obviously it matches. Well then else, that means it doesn't match.
But check out what happens here if I just print a simple statement.
So now if I hit run and we take a look on the right side,
you can see that if I guess a,
then you can see that we get 'no match' printed five times,
and that should show you that this is actually not where we want to write the
code for to-do number two,
because remember to-do number two says if the guessed letter is not a letter in
the chosen word, then we should reduce the lives by one. But actually in fact,
what's happening here is it's looping through the entire word length,
and for each of the letters in that chosen word,
if the letter doesn't match the guess,
then it's going to trigger this else statement.
This is not what we want because in this case
a does in fact match a letter in the word baboon,
but we're getting no match being called five times.
So instead, we wanna write it outside of the for loop,
so at the same indentation level,
and the hint is that the to-do is actually at the same level as where your
answer code needs to go. So here,
we're going to write an if statement and we're going to check if the guess is
not in the chosen word.
And basically this does a separate check.
It goes through this entire word and checks
if the guessed letter exists in there at all.
And if this is true,
then we're going to reduce the number of lives by one, so -= 1.
Now, if at this stage
after we've taken a life away and the number of lives goes down to zero, well
then the game should stop and it should print "You lose."
So let's add another if statement in here that checks whether if the number of
lives is equals to, double equal to, zero.
And if this is the case, then we should exit the game. And remember to do that
we need to exit the while loop.
And the while loop is dependent on this variable end_of_game
which starts out being false. And when we want to exit the game,
when it is in fact the end of the game, we switch it to true. Like,
for example, in this case, when the user has won. So here,
when the lives is equal to zero, we can say, end_of_game = True,
and we're going to print "You lose."
So that's all that's needed to tackle to-do number two.
So now let's run our code again.
And you can see that if I keep guessing the wrong letter six times,
then eventually I exit out of my while loop.
You can see my prompt showing up again.
And this means that we've now come out of the game and it's told us that we've
lost.
So now all we have to do is tackle to-do number three.
We have to print the ASCII art from the list called stages
that corresponds to the current number of lives the user has remaining. Now,
luckily for you,
I've arranged each of these pictures in descending order of lives.
So when you have a full six lives,
the image at index six is the starting image.
And when you have no lives,
so the image at index zero is the one where the hangman is fully drawn.
So all we have to do down here is to simply just the print
the stages,
and corresponding to the current number of lives.
So we put lives inside the square brackets.
And now if we run the code again and we keep guessing,
you can see that we're going through our hangman picture until we are fully
drawn and we lose and it's the end of the game.
Have a quick review of the solution
and if you want to see this code that I've written,
then as always, head over to the course resources,
and you'll find a link to Day-7-Hangman-4-End
and you'll be able to see all the code that I've written in there, including,
um, helpful things like a print statement that you can activate,
which just shows you the current position,
the current letter and the letter that's being guessed.
So you can see as the loop is going on
what's actually happening and it'll make it easier for you to debug any problems
in your code. So if you had any problems in the code that you wrote,
this is a really useful line to be able to help you debug what's gone wrong.
Hopefully you manage to do thi okay,
and once you've had a quick review and you're happy with everything that's been
covered in this lesson,
then head over to the next lesson and we're going to complete the final step,
step five of our hangman project.