All right.
We are very much now in the home stretch.
We are on the last step, Step Number 5 of five.
And we're going to complete this game in this lesson.
So when you open up the starting file in the main.py, you will notice that there are a bunch of errors.
And this is intentional.
Now what's happened is I have deleted the list called stages.
I have deleted the list called word_list, and instead, I want you to get them from these files hangman_art,
and hangman_words just so that we can practice using other modules.
And this is something we learned in previous lessons as well.
So if you forgot, then take a look at Python modules when we introduced the Random Module.
So that tackles TODO Number 1, TODO Number 2, and TODO Number 3, which is to basically
use what you've learned about importing modules in order to bring in those things that we need for
our code to function correctly.
Now the next few TODOs are mostly to do with tidying up the logic and also just making our app a little
bit easier to use.
You'll notice I've added some extra bits in here, like some asterisks, just to make each section when
they lose, or when they win a little bit more obvious.
And in Step 4, I want you to give the user some feedback to improve our user experience.
So if the user has entered a letter that they've already guessed, we need to tell them, "You already
guessed this." And we'll print that letter and let them know.
But we're not going to deduct any lives because I think that's maybe a little bit unfair.
So an example is you've already guessed letter a, something like this.
Now TODO Number 5 is on a similar vein.
If the letter they guessed is not in the chosen_word, we're going to print it out and let them know.
So give them some feedback.
For example, "You guessed d, that's not in the word.
You lose a life." Just so they know exactly what's going on in the game.
And the final TODO, TODO Number 6, is to update this print statement so that we give the user an indication
of how many lives they have left.
So it'll be 4 / 6 lives left, then 3 / 6 lives left,
So each time we rerun our while loop, they know how close they are to the end and give them a little
bit more danger and anticipation.
So these TODOs are a lot easier than previous ones, and I think you'll be able to tackle them just
fine.
So pause the video and complete your project.
Okay.
Let's start with TODO Number one.
So you might remember that we can simply import any module,
and this module is called hangman_words.
And it looks like this,
it contains just a single list called word_list.
So we can do it in two ways,
we can do it the usual way, which is how we imported random.
So we import the module which is hangman_words,
and then we can use it inside our file.
So here when we're using word_list, to get rid of this red underline, we can say hangman_words.word_list
.
And then Python will be able to find this file because it's imported.
And then find this word_list that we need.
Another way of doing imports is a way where you can avoid using these dots and makes the code just a
little bit tighter.
So instead of just writing import hangman_words, you can say from
this module called, hangman_words, import this one thing, which is the words_list.
Now of course, if you have lots of things to import from a particular file or module, then this is
going to be a little bit painful,
but in our case, when there's only very few and we only want to import one or two things, then this syntax
can make it a lot lighter.
And it means we don't have to write hangman_words.word_list every time we want to use it, which
can be a bonus.
Now, of course, you might not have seen it this way.
So if you complete it the other way, it's also perfectly valid,
and in most situations, that's exactly how you want to do it.
All right.
Let's go ahead and knock off TODO Number 2, and TODO Number 3.
So TODO Number 2 we just need to do the same thing and import.
So from hangman_art, we're going to import the list called stages,
and then that gets rid of that error right there.
And TODO Number 3 we need to import the logo which we've got right here called, logo.
And this just means that when we start up our game it will display a nice little logo.
So we'll just print and we're going to print the logo.
And then we're going to import the logo.
So we can also add a new from whatever,
but it's actually much easier to add a comma and just add logo afterwards.
So this way you save yourself a little bit of code.
But if you wrote it like this, hangman_art.logo,
that is also perfectly valid.
Now Step Number 4 is when the user has entered a letter they've already guessed,
we're going to print the letter and let them know.
So this is the example and I'm going to use it.
So if the guess, so the letter that we're guessing right now is in,
so the 'in' keyword, to check if it exists inside the set of correct_letters,
well, then that means that they previously already guessed that letter.
So we need to let them know, print that they've already guessed that letter. So we can copy this
line and paste it in here and use an f-string to insert the guess right there.
So now if we go ahead and run our file as it is, you'll see,
firstly, we get our nice little Hangman logo printing.
And I've got the chosen_word still printing right now because otherwise, it's impossible to test this.
I'm not good enough at Hangman to be able to do this.
So we've got our word to guess, and then we have to guess a letter,
So I'm going to guess r, and you can see if I guess r again I don't lose a life,
and it just tells me that I've already guessed r. So that works as expected.
Moving on to the next TODO if the letter is not in the chosen_word, then we're going to print it out
and let them know.
So it's important to give user feedback because that improves the user experience.
Now there's a lot of science and art to user experience, but it's good to start thinking about this
even as we're learning the basics of Python.
So if the letter is not in the chosen_word, so that is under this if statement, then we're going to
reduce their lives by 1,
but we're also going to add this print statement.
So I'm going to copy the example as it is.
And then I'm going to paste it into this if statement and replace this with an f-string, which is the
{guess}. And very thankfully, Python automatically adds this f in here because I'm using PyCharm and
it's a really, really helpful code editor.
But just be aware that that's what's happening just now.
Moving on to TODO Number 6, we're going to update this part, this print statement to tell the user
how many lives they have left so that each time the while loop runs, they can see how many lives they
have left 4/6, 3/6, 2/6,
give them a little bit of a feeling of impending doom as any good game should do.
If you play Elden Ring, you'll know what I mean.
And inside here we're going to insert the variable called lives.
And again, this should be an f-string.
And we only have one more TODO left before we finish this game.
So update this last print statement to give them the correct_word that they're trying to guess.
It's never good to frustrate your users too much.
I'm looking at you from software, the people who make Elden Ring and making it really difficult to
pass all the levels,
but here we want to be able to give the user the word that they've been trying to guess all along.
So even if they don't get it and they lose, they at least know what it was.
So we're going to again insert this as an f-string.
And I'm just going to copy the format here in the TODO, telling them that it was this particular word,
replacing this with the f-string, and my variable is called chosen_word.
And now I have completed the entire project of Hangman.
Now, I'm not saying that there's no more improvement.
There's a lot of things that you could do to improve Hangman, and this is by no means a fully-fledged
game.
It is just an exercise to be able to test and revise some of the concepts you've learned about
Python up to this point, and hopefully it was fun building it,
and hopefully, you're going to have some fun playing it and showing it to friends.
Now, there are a ton of more improvements that you can make to this game, and if you're up for it,
then you can of course go ahead and make it to your heart's desire.
Change it, or update the user experience,
change the wording,
give it more functionality, different functionality, whatever it is you want to do, the world is
your oyster.
But hopefully, you've managed to understand all of the code up to now,
and if you don't, just try deleting everything and starting from the beginning until you make sure
that every single concept in here that you've reviewed, either you've gone back to previous videos
or you've looked up documentation,
as long as everything makes sense before you proceed.
I know it's really tempting to keep to the one day, a day kind of format, but the more important
thing is that you're actually learning valuable skills and you're not just skipping ahead.
So I hope this was fun, and I hope you'll look forward to building more stuff with me in future lessons,
but right now, take a rest and well done.