4.5 Write a word-guessing game - Video Tutorials & Practice Problems
Video duration:
21m
Play a video:
<v ->Okay, so we've covered all of our basic,</v> fundamental topics, so far. And now we're ready to work on challenge three, this word guessing game. You might know it as Hangman, but I think that's really morbid and weird, so we're just gonna call it a word guessing game, but it's got similar rules. You're going to, we've got a list of words and you're going to choose one at random. The user tries to guess the word one letter at a time, and they have six wrong guesses before they lose. So, just like in previous lessons, you know, feel free to pause this video, work it out on your own, see where you get stuck, Google things if you don't know what to do next. But I'm gonna now go through this in just, kind of like what pops to my head. So the first thing we're gonna do, actually, is write some comments down and try to break the problem into smaller steps. So, at the top, so in this challenge three, word guess.py, at the top, I've actually found a list of words, and put them in here as a list. And then I also found a list of hard words for Hangman, and I've put them here too. So you can choose which difficulty you'd like to play at. So in PyCharm I can click on this arrow, this minus arrow, and that will hide things so I don't have to see it anymore. Because you also don't wanna know what the words are, so then it's more fun to play the game. Okay, so the first thing we want to do is, ah, pick a random word. And then we want to display to the user like, how many letters there are. So. And that's gonna look something like this. So imagine our word was: hello. So, let's display something like this, where it has the number of letters, and each letter that hasn't been guessed is just an underscore. Display the number of wrong guesses they have left. So that's six. Okay, and then we want them to guess a letter. And see if it's correct. So if letter is in word, tell them it's correct. And, ah, if not, say: wrong, er, that's a little harsh. Say: nope. And then decrease number of wrong guesses. And then we want to check if they won, and also check if they lost. If they lost, let's see, tell them the word. And then if they won: you win! Exit. Tell them the word. You lost. Exit. Okay. So we're going to repeat all of this stuff until they have no wrong guesses left. So, it is a, it is gonna be a fairly long program. There are quite a few different steps in here, but we know how to do quite a few of these. So the first one, pick a random word, we know how to do that. So we're going to import random. Oh, actually, we wanna use a test word of: hello. So, let's pick a random word. Let's say: answer equals random choice. And we're gonna pass in one of these lists. So let's pass in that words list. So, that's the actual answer, but our test answer is just gonna be: hello. And so we can just, like, comment this out. I can quickly comment out lines by pressing command, or control-slash, in PyCharm. And that will toggle between things being commented out and not. So I'll uncomment this when we wanna use our actual random words. So for now, let's say the answer is hello. And then we want to display to the user this. So, one thing that we can do is loop through each of these letters and print out an underscore. So, for letter in answer, then we need to build a new word. So I'm going to create a new one here. So, display word, is going to just be the empty string. And then I can add to display word, plus equals underscore. And a space, because we wanna have a space in between these. Okay. So now we wanna show it to the user, and I'll print: display word. So, let's run this. Now it looks right; H-E-L-L-O. It is going to have an extra, ah, space at the very end. If we wanted to remove that space, we could actually use a string method called: strip. And this will strip like empty characters, or like empty spaces from the beginning or the end of the word. So I can do that, and now it's just like that. Okay, so there are other ways of doing this, but this is using methods that we've already seen. So that's good, and then we wanna display the number of wrong guesses they have left, so we need to, we want them to have six wrong guesses available, so I can put that at the top. Num wrong guesses left equals six, and then I can print that here, too. F, num wrong guesses left. Wrong guesses left. So, run that, and six wrong guesses left. So that's good. Okay, so I'll delete that comment. And I'll delete that comment for now. And then we wanna ask to guess a letter. So. Guess is gonna be input. Guess a letter. And then, we're gonna see if the letter is in the word. If guess in answer, print: correct! Else, print: nope. Guess a letter; E, we know that's there. That's correct. X, is not. Okay. So we also wanna decrease the number of wrong guesses left. If it was wrong, so, ah, num wrong guesses left, minus equals one. All right. So, let's pause this for now, the checking if they won, and we want to loop over all of this. So we can say: while, maybe let's for now say while num wrong guesses left is greater than or equal to zero, then loop through this stuff. And this is actually also going to be in the while loop, because if they won or if they lost, we're gonna exit. So, let's run this; let's see. Guess a letter, E. Correct. So we can already see that we probably want to fill this in with an E, if it's correct. X, nope. We have five wrong guesses left. W, W, L, that's correct. W, W, W, nope. Okay. Then we can, we also here I accidentally pressed enter and didn't enter anything, and it said it was correct. So, there's a few things to do still. It did exit at zero wrong guesses left. So, that's good. Maybe the first one to work on is getting the correct guesses to display in this string. So, I've got this display word, and this is the letter, and right now we're ignoring the letter that we're looping over, and just displaying this underscore. But we wanna say: if they've guessed it already, then display it. So we're going to need a new variable in here that adds all of the current guesses to the variable; so that could be a list, and that's one that we've seen already, so we can do that. So we can say: guessed letters equals an empty list. Then if, ah, so if the guess is an answer, or it's wrong, we can actually add the guess to the guessed letters, every time. So guessed letters, plus equals, or, no, append. Guess. And then, here, instead of adding an underscore, we can check if letter is in guessed letters, then display word plus equals that letter. Okay? So we want to add the letter, if they've already guessed it, but if they haven't we want to add the underscore. So I'm now going to change this to else. If they haven't guessed the letter yet, then display word plus equals the underscore. And then we wanna add this space in between, too, so we can actually just add that outside of the if or else statement, and always add an underscore to it. So let's see this now. So I can guess a letter, I can guess an E. Great, and now it's there. I can guess an L, and it happens for both of the L's that exist in the word, and I can guess an A, and it doesn't change anything. And I can guess the full word, and that's cool, but it hasn't told me I've won yet. Because we haven't coded it. So let's stop that now. So let's check if we won. So, how do we see if that's the case? So, we can check if all of these letters are in the guessed letters. That's one way. So, if, how would we do that? So, we can say, we can have like a variable. Won equals false, and for letter in answer, if the letter is in the guessed letters, oh, actually, yeah. So we can say, we can assume that they've won. And then if the letter in the answer is not in the guessed letters, then won equals false and break. So now we can see if they won, then print: you win. And then return. Now we can check if they lost. Elif, ah, so they'll have lost if their num guesses left, actually, maybe that'll work in the, if we get out of the while loop. So, print: sorry, you lose. Print: the answer was this. And then we'll make it an F string, call it: answer. So, let's see. H, that's correct. E, L, L, O. Correct, I won; that's great. And then X, X, X, X, X. Okay, I've lost. So it says: zero wrong guesses left. This is still going at zero, so let's get rid of that. Nope, sorry, you lose. The answer was: hello. So that's pretty close. It would be nice to show what letters have been guessed. So we can just add that too. Where do we wanna add it? Maybe here, after wrong guesses left. We can print, guesses, and then just print the guessed letters. So that people can know if they've already guessed something. Great, and then lastly, we can swap this out, and use an actual random word. Okay, so let's play it. A is not in there. E is not in there; oh, no. Okay, maybe that ends in an N. Cool. C, oh, I think it's: condition. Yay, I won! Great, so ah, now that we've got our base word game, this might not be how we want it to look in the end, so we can move things around, refactor it, add some tests. And we'll see how to do that in the next lesson.