All right, so let's go through the solution together.
Now we know that the part that we need repeating is this part where we ask the
user to guess a letter again and again
until they filled out all the blanks in this display.
The first step is to wrap a while loop around this block of code so we can
select the entire block of code and you can either indent it individually or
just select all of it and hit command and the right square bracket.
And that will shift everything by one indentation level
and that effectively puts it inside our while loop.
So now we have to define our while loop.
So while something is true or while something is false,
repeat this code again and again. So what exactly is our condition?
We know that we only want our loop to end once there are no more blanks
contained inside the list called display.
Why don't we create a variable which we'll call end_of_game and set that to
false
to begin with because it's not yet the end of game. And while it's not yet at the
end of game, so while this is false. So you can either say while
end of game is equal to false or alternatively
and I think slightly easier to read is you could say while it's not at the end
of the game, then repeat this continuously. Now,
if we were to run our code right now
it would actually loop on until forever.
It would not stop because nowhere in our code,
do we actually flip the switch and change this true.
So when is it actually the end of the game? Well,
it's when display has no more blanks.
So why don't we check at the very end after we've printed the display to see if
blanks are not in display.
So remember this in key word comes from Python and it basically allows us to
check to see if a particular element exists inside a particular list. So in this
case, we're checking if curly exists in list and there it is.
I can see it as one of the elements. So this is going to be true.
Now, if blank
in list is no longer true, well,
that means that we've gotten rid of all the blanks and we replaced them with
letters that the user has guessed.
So now this is the condition for the user having won.
So we can flip the end of game to true
and this will mean that this is last time
our while loop is going to run and we're going to print "You win."
That's it, that's all we needed do. And if it's helpful,
you can again, take this code, put it into Thonny and run through it
using the debug step-by-step to see what it's doing. Now
the important thing here is to realize that while statements require some sort
of change to happen inside the while loop
that changes the condition that the while loop is dependent upon
and it means the code exits the while loop. So in this case,
it's when there are no longer any more blanks inside the display list.
But in later steps,
we're going to change that so that when the users has run out of lives,
then it's also going to be the end of the game and they will have lost.
So take a moment, review this lesson,
make sure you understand everything that's going on.
Then head over to the next lesson and we'll tackle the next step.