All right.
So now that we've reached Step 3, we are moving onwards and upwards.
And we're going to add a little bit more functionality to our hangman game so that it functions more,
and more like a real game.
So in this particular step, we're going to add the ability for the user to continue guessing again, and
again.
Repeating the hangman checks until they get all the letters in the word.
So if you go ahead and right-click on solution.py, and then run that solution, we can see what the end
result of this step will look like.
So we have the chosen_word,
we have the number of characters in that word,
and then we are asked to guess a letter.
So if I start from the beginning and I guess "c", now what we want to happen is this we're getting another
opportunity to guess.
And we want this to repeat by writing a while loop in our code.
So that's TODO Number 1.
Now the next thing we want to do is to update this for loop here, so that if we guess a previous correct
letter such as the C in this case, when we guess another letter correctly, we want to keep the previous
correct guess and the new letter and add all of that to our variable called display.
Now, in contrast, let's take a look what happens at the moment if we use our current state of our
code in main.py.
So if I guess "a" and all the A's in aardvark gets filled in, the next time that I guess this
word, my previous ones will be lost because I'm again checking against the current guess.
So think about how you might solve that.
And if you get stuck, there's always hints in the Description box for you, but there is quite a bit
of thinking and quite a bit of struggling in this particular program that you're writing.
But as always, it's really good when you struggle because that means you're learning something.
If you're not struggling at all, then this part is probably too easy for you and you need to skip ahead
quite a few days.
But if you are struggling, then that means you are at the right stage in your programming journey and
you will eventually get to where you want to be.
So I'm going to go quiet now and let you have time to think.
So pause the video, read the Description, try out the solution, run, and see if you can figure out
these two TODOs in step three.
All right, let's go through the solution.
Now, if you haven't attempted to solve this, I recommend to stop, and don't watch the solution just
yet.
It's only worth watching the solution if you've actually tried at least for ten 20 minutes,
because otherwise, the solution is just going to be somebody reading to you a bunch of code and
you won't do any learning.
And I'm really, really against this idea of spoon-fed education where we're just told what the
answer is, because without the thinking phase, we're not truly learning, we're just repeating somebody
else's thought.
And the most important thing to learn is how to think.
That's what a really valuable program of skill is.
Okay, so I believe you.
You've already tried it.
Now let's see if we can go through the solution.
So the first step is to use a while loop to get let the user guess again.
Because at the moment once they make this one guess, then everything continues and we are at the end
of the script.
Now, how do we make them guess again?
Well, we would need a while loop.
So while some condition is True, or while some condition is False, then we want to continue running
a block of code.
And then when that condition switches, we skip out of the loop and we continue in our code.
So what is going to be that condition?
Well we can have a condition called, game_over, and we can set that to False.
Now, we can check for that condition.
We can say while not game over,
so while game_over is False, we want the user to make some guesses,
we want to update this display,
and we also want to do the rest of the code because we need to check through the letter of their guess
and see if we can update the display, et cetera, et cetera. So all of the rest of the code actually go into the
while loop, and by go in,
if you remember, it means that they get indented one step so that they're inside this while heading.
Now when do we actually change the while loop so that they can come out of it?
Well, if we have these blanks in our display to represent the unguessed letters, if all the unguessed
letters are guessed and there are no more underscores, well then, at that point that's when our game
is over, right?
So let's add an if statement and check if display...
And there's a really useful function in Python which is called in, and what we can do with in is we
can check to see if something exists in something else.
So we can say if this underscore ( _ ) is not in our display variable,
so this string does not contain any of these underscores,
well at that point, they've guessed everything because there's no more blanks.
They've guessed all the letters.
Then at this point, we can print.
"You win."
And that's also the right time point when we can change our game over so that we set it to, True, because
they've won,
therefore, game is over.
Now we're going to define other conditions for game_over later on,
but for now, at least, we have one place where our code can exit the while loop, which is really,
really important when you're writing while loops, because otherwise you get into this infinite loop
situation and that's not good.
So let's test our first TODO and see if it does what we expect it to do.
So aardvark, let's guess a letter, "a" a gets filled in and then it's asking us, Guess another letter.
Right?
So let's keep typing because we know what the word is.
But now we encounter another issue,
because notice how the "r" is being placed into our display variable,
and the rest of them are filled in with blanks,
but our previous guess, the "a" has disappeared from the display variable, and it's replaced by blanks.
So in this particular situation, we'll never get to the state where there are no more underscores in
the display, because each time this while loop runs, we're only ever checking one letter, which is
the current guest letter.
So to fix that, we have to go into TODO Number 2 and change the for loop so that we can keep the
previous correct letters in the display string.
So how do we do this?
There's many ways of doing this,
and one way of doing this is simply by creating a list.
Now notice I'm going outside the while loop to create this list, because I want this list to accumulate
while the loop runs, so that each time it's not being wiped blank.
So if I create a new list here, which is called correct_letters and set it to a blank or an empty list
like so, if this was placed inside the while loop, then every time the loop repeats, that list is
going to be wiped to zero because of this part here in this line.
So instead we want it to be outside the while loop.
And now what we're going to do is if the letter that we're looping in through the word, is matching the
guessed letter by the user, then not only do we add that letter to the display, but we also add that
letter to our list of correct letters.
So correct letters.append() and we're going to append this current letter that we're looping through.
You can also append the guess.
But at this point they are exactly equal.
So it doesn't really matter which one you choose.
It just depends on which you think is more readable.
Now if we rerun this code, it doesn't really change much, right?
If I change a letter, it's still not doing what I want.
So what I have to do also is probably add another check.
So I'm going to check another condition using an elif.
So if the current guess matches the letter that we're currently looping on, then we add the letter
to the display and if it doesn't match, then we add an underscore.
But there's another condition we need to check.
We need to check, elif, the current letter that we're looping in, is in the list of correct_letters.
Because remember, if on a previous run of the while loop, a letter was guessed to be correct, then
it gets added to this list of correct letters.
And so now whenever we check our letters and we want to fill in the other letters in the display, we
need to make an elif.
So in this case, what are we going to do?
We're going to add the current_letter to the display.
So similar to this line here.
So now if we rerun this code and we add a letter.
So now "b" has been added to the display,
let's try "n".
So you can see now b is added but n is also added.
So the flow goes like this.
The four letter in chosen_word,
the first one that gets turned into a letter is b.
So we're checking is b equal to the guess at this point?
Yes.
So then we add b to the display and we add blanks to the rest of it.
Now then the correct letter is added to this list.
So b now lives inside this list.
And notice it's outside the while loop so it doesn't get modified when the while loop repeats from the
top of the block of code.
So the next time this runs, we're guessing a different letter,
we're guessing "n". So when n is true, then we are adding n to our display,
but when something else is not one of the letters that we're looping in, it's not matching guess, for
example b, but it is in fact inside the list of correct letters,
then that letter also gets added to the display. And this might be something that you want to try and
run in Thonny if it is confusing, but if it all makes sense, then feel free to continue to the next
step in the game.