 Okay. Hi everybody. Welcome to my channel and today's lesson is going to be how to make a very simple Hangman game. This was a request from some of my students and they are just really getting started with programming So they don't know a lot of the advanced features. They know a little bit about input They know a little bit about if statements and they know a little bit about loose But not a lot and that's about it at this point So I'm gonna try and make this as simple as possible But hangman has a little bit of a complicated thing with the way the strings work And so I'm gonna try to walk you through that as simply as possible. So let's go ahead and get started Just real quick. If you haven't subscribed, please do subscribe I would appreciate that so you can get some updates because I do post a map semi regularly and Also, just I get asked this question a lot. I am using Genie as my IDE. It is free It is open source. I'll put a link in the description down below and I'm programming this in Python 3.8 So you can follow me on Twitter at Tokyo EdTech or here. I'm assuming you're watching this on my YouTube channel So let's get started First thing I do is I'm gonna import OS and I'm gonna do OS that system Clear because I want to clear the screen now if you're on Linux like me I'm on Ubuntu. You just use clear if you're on Mac. You can use clear here windows. I think it's CLS It could be CLR, but probably CLS, but We're gonna use clear So the first thing we need to do is to initialize our game Initialize just means to set the initial values So again, this is hangman So what I'm gonna do is I'm going to choose a word that needs to be guessed in this case I want to do Ubuntu Just because I'm that's what I'm using and that's kind of interesting word and then what I need to do is I need another variable to Hold the content of our guess. So I'm gonna say guess equals In this case, I'm just gonna make it The same size as Ubuntu. So one U, B, U, and T, U And later I'll show you how to you know make this a little bit more dynamic But you can see here We've got one two three four five six letters and here we got one two three four five six dashes And the dashes are gonna be really really important and of course, this is hangman So we need to be able to keep track of the wrong letters that we are Saying so I'm gonna go ahead and Make that a little empty right now. It's empty because we haven't added any letters to the game, all right, so Next up, I'm gonna go ahead and print the header And this is just really just the title. So I'm gonna say and print hangman, you know and just Yeah, maybe leave a space there Alternatively, I could put a forward slash in here to do the same thing and Since this is a game and we're gonna have multiple guesses we're gonna need a loop so I'm gonna use something called a oops a Wild true loop so main game loop and this is while true So what I do is we want to print The current guess Actually, I'm gonna use something called an f string here. So I'm gonna put f there and then I'm gonna put guess Okay, so let's go ahead and Actually, I'm gonna run that yet because it's gonna run forever So I'm gonna go ahead and go I'm gonna ask the user to enter a letter Please enter a letter and notice I called it letter because I'm entering a letter It I could have called this X, but X doesn't make a lot of sense here The only thing I want to do is notice how the word is all capitals So I want to make sure that this is capital as well So I'm gonna use the upper method and no matter what I enter that will convert it to an uppercase letter Let's go ahead and test the code at this point Okay, so here's our current guess now it will print the hangman later so don't don't stress out about that and Please enter a letter. I'm gonna go ahead and hit you. Yeah, of course. There's no code to do anything But you know, that's where we get so I'm gonna go ahead and control or command Z If you're on a Mac or control Z on Linux and Windows notice here in genie This is something I don't like and I haven't figured out how to fix it yet. The terminal is red And there's a might change color. So if I try to run this again I'm gonna get this error message file not executed because the terminal may contain some input Control C or enter to clear it. So we go ahead hit enter Didn't work because the focus is up here So I click down here in the terminal and hit enter really annoying and if anybody knows a way to Stop that from happening. Please. Please. Please comment below. Okay, so we've entered a letter So the first thing we got to figure out is If this letter is in the word Fortunately in Python, this is very very easy to do. So I'm just gonna put Check if the letter is in the word. Okay, so I'm gonna put if letter and word It's that simple in Python. Thank you, Python. We love you So if the letter that I just entered is in the word, which is the correct word I'm gonna go ahead and just say print Let's see, okay, it's a good guess and then else because it's either in there or not print bad guess And later we'll we'll fix this So I'm gonna go ahead and run it. I'm gonna test that and see if it's working This is something that beginners do that drives me up a wall Is they'll watch a tutorial like this They'll type the entire program and then they'll start Testing it and you need to test your code at every stage. Let's go ahead and test it Oh, I already ran it didn't I? So let's say Let's try you because we know it's in there. Good guess. Let's try q because we know it's not in there Okay, so I've tested it So I know I've got a good guess and a bad guess and that this part of the code is working So if I run into problems from this point I'm pretty sure it's not going to be this section randomly adding stuff But this tells us that up to this point the program is working As we expect it to So this is where it gets a little bit more complicated and this is stuff that I know my students haven't seen yet So try to follow along as carefully as you can. There's a couple new things in here So what we need to do Is we're going to use a temporary variable And we're going to make it an empty string And we go ahead and delete this because I don't need that anymore And the temp is going to be used and we'll see what I do with this So think about what action actually has to happen here. I've got a word I've entered the letter u So I've got it. So I've already figured out if it's in there or not and now I know it's in there So in the case Of u it's there here here and here So what I'm going to do is I'm going to use something called a slice So I'm going to check every single letter one by one I'm going to start at zero Slice indexes start at zero So this is the zero with character. This is one two three four and five Notice the actual length is six. There's six letters But the first u is zero b is one u is two three four and five So watch what I do here For index so the index would be zero to five in this case in range The length of the word And notice I notice the balance of the parentheses. I got a parenthesis here here and here and here colon So what that does is for each Index so zero one two three four five We need to know if the letter Equals word Index So in the case of u Does u equal word zero? Yes, it does Does u equal word one? No, it doesn't Does u equal word two? Yes, it does Does word or does letter you should say does letter equal word? One zero one two three. No does it equal Four no does it equal index five? Yes, it does So if it does equal We're going to add to temp the letter That's the first case. It does equal else Or else if there's a second case If the guess Index is our previous guess Does not equal a dash It's wrong line. Sorry about that So that means that we've already found a letter. So like let's say we enter u the first time then we enter b Then we add to temp plus equals guess Index Okay, so if the letter it does not equal What's in there? Now we might get a weird error if we enter a letter twice We entered u two times, but we're going to assume that nobody does that Um, which is always a bad thing and so then Otherwise temp plus equals we got to add a dash back now watch the That the the indentation here. I messed this up when I was testing this program earlier I had to restart this video. So I was very annoyed. Um So guess equals temp And actually what we should go let's go ahead and try that and see what happens Okay, so I'm going to enter a u So we know that the u is here Here and here So I'm gonna go ahead and run that all right ran it. I'm gonna go ahead and enter Okay, so you can see we've got current guess u space u space u Okay, let's go ahead and try b Okay, so far so good. Let's try a letter that's not there e Okay, so we're still getting bad guesses And we can do Okay So now The basic program is working. That's that's essentially hangman. Uh, the rest of what we got to do is Basically, uh, just yeah make it prettier. I suppose. So this is the hard part of hangman. Everything else is really easy Um So let me walk you through this one more time So what I'm doing is I'm getting a letter from the user Okay, I've got a word And I've got a guess that has some dashes in it and dashes are really important So I'm checking each index. So in this case from zero to five Does the letter that I entered equal the index that that letter at that index So does u equal u if so temp which is empty right now Gets added that u Okay, then we go back around to this and we go to the next letter b does u equal b no Okay, um, is it not Is it not a dash Then we add Actually, let's let's we can switch this around make it a little bit easier now. I think about this Um, well, okay, we'll leave it the way it is. It's let's leave well enough alone This is actually probably better just for you to think through So if it's not a dash that means we've previously guessed that letter and we're just gonna add that letter again We don't need to worry about it. We've already done it okay, and then Otherwise, it's not the correct guess. It's not one we guessed before So we just added the dash to the temp and then what we do is we've just built a new temp And then we we change guests to the current temp That's it. That's it. Let's uh Yeah, that's it. Okay, so How do we figure out if the user has correctly guessed it? So check for a winner So basically we've got the word And then we've got a guess So if the word equals the guess print you win And you live To play another day Since it is hanging And then what we want to do is we want to exit the program and we just use the exit command Okay, so let's test that again notice type little code. I test it. Okay, so I'm gonna do you B and T and what should happen Boom you win and you live to play another day. Congratulations So there we have the winner code now Now I need to deal with what we're gonna do if there's a wrong answer Okay, so you can see here. We've got wrong letters. I created that earlier. We haven't only used it yet so what I want to do with that is I'm gonna say print Uh wrong. Oops. Yeah wrong guesses I'm gonna put here wrong letters Okay, again, this is an f string so When I get something wrong, I don't want to print bad guess I'm just gonna go ahead and say was it bad wrong letters plus equals Letter because that's what I did. I just entered the wrong letter. So let's go ahead and test that Okay, so I'm gonna try you again. Make sure that's still working I'm gonna try Q Okay, so you see now I've got Q as my wrong guess and this is what I have my current guess as Um B. Let's try let's try R Okay, so you can see how it's building up The wrong guesses now again the formatting here is not very good But you get you get the idea. So if we scroll up a little bit you can see Anything I enter that's wrong. It's coming up with wrong guesses Let me go ahead and put there's just like a forward slash in here So it's a little bit easier to follow and maybe we'll put uh Yeah, I'll leave like that. Let's try that just yeah, it looks a little bit easier. I think let's try you Okay, that's that's much better. Um Let's try some wrong guesses. Okay. So now we got some spacing and it's a little bit easier to see Okay, so the next thing that's left is to deal with Losing Okay, so we have to decide How many wrong guesses we're going to give the user? Okay. Now when I I was playing around this program earlier I think I came up with I think it was going to be four or five. So let's let's say five for now Um So When we start the game Well, let's say let's let's check. Let's check for a loser. Um, is that first check for a loser? So how we're going to do this we're going to keep it really simple Notice that every time we make a wrong guess we're adding a letter to wrong letters So because of this we can use the length of that as our Kind of our meter. So if the length of wrong wrong letters Say equals five print You lose Sorry, sorry, sorry sucker. Okay, and then we'll exit the game Okay, so let's go ahead and test that I gotta clear that again Okay, so I'll try my u so make sure that's still working a b c d e And here should be my fifth letter And my game is over Okay, so this is base. This is hangman. Okay, we've essentially done the program Now the only thing really left to add is to print out the The hangman the hangman graphics that's gonna be pretty straightforward. So I'm gonna say print the hangman print the hangman Actually, well, okay I'll leave it there and then I'll show you why it's not a good spot for it. So what I want to do So if the length of wrong letters Equal zero So this is going to be our empty gallows I should say what we're gonna do Probably the easiest way to do this Is actually to do it the other way the other way around So if length of wrong letters equals five We'll do the full one first print Now watch what I do here. I'm gonna put three quotation marks Then I'm gonna come all the way back to this side I'm gonna go ahead and make my gallows I think that should be big enough and One two three and then parentheses Now what I want to do here is I want to make my final Gallows So I'm gonna go ahead and do the head I'm gonna do the body Let's see and see feet so feet and Then we'll give this person some hands now if he's dead Maybe your hand make the hands down here. Okay, so this is End of game Okay, now those of you have programmed before you can probably see there's a slight logic error But that's okay. We're gonna fix that a little bit So this is my base. Okay, and then what I want to do is I'm gonna go ahead print this here Now I could make these ls, but we'll just leave it as it is Again, there's still a couple little problems here that we're gonna fix in a bit and so five four three two one And remember when we start the game The length of wrong letters is actually zero because there are no letters in it So in that case, I need to delete Okay, so this is how the game starts. There's there's nothing in the gallows Okay, and then game progresses I get one wrong answer and we'll put the head in Okay game progresses and I need to delete Put the body in Game progresses and we need to put the arms in That's how I'm gonna do it. I'm gonna make it a little bit stricter The game progresses and we have all of the body parts And then what I would do probably here is I might delete this and add a level here And then add like Yeah, the rope And actually what I want to do I should have done this differently When while while the the character's still alive, I'm gonna keep his hands up And then if he dies then his hands will go down So that gives that gives us a little bit of you know, kind of a little animation there Good animation or a bad animation Um, so let's try this. Let's see what happens Under a letter. I notice we didn't print that out yet. So I want to go ahead and hit q Oh, there's my head Okay, and do you Okay, I'm gonna try a b c d Now notice how this didn't come out correct and we'll fix that in a minute and I got one more guess basically e Okay, now notice how it didn't print out and notice how that leg is is a little bit messed up. Okay, the leg is messed up Because we need to With we should probably do it here too Forward slashes or these backslashes. I forget which one they need to be doubled up and this is just a Weird little computer thingy. Um, and so that I need to do the same thing for the foot here Okay, I need to fix those And again, remember how it didn't print the last one because we checked for a loser here And then we exited the program So let's go ahead and print the hangman first Then check for the loser. We're gonna put that at the end The other thing I want to do Is I want to at the beginning of the game I just want to print out one Hangman And that should not be indented. Okay, so let's go ahead and test that Make this a little bit bigger Okay, hangman current guess I'm gonna go ahead and do my u Alrighty wrong guess. Okay, so let's do a b Oh b's in there says c. Uh-oh d He's still alive. Oh my gosh. E. Oh my gosh. And then last one He's hung. Okay, that kind of looks pretty good. That was good or bad. Um, maybe we can make the uh legs Uh straight at the end This poor guy, um Let's try that. I'm gonna do that one more time. Um I really like these old school, uh graphics c d e f and here we go Oh my gosh, this poor guy Yeah, I forgot that there so that kind of screwed things up Um Okay, also when you lose we should probably print out the correct word print And I use an f string the word was And I'll say word Again, I'm adding stuff. I gotta test it Which is not the worst thing the world will do here Okay c d e And f okay, and there's our poor Dead person. Okay, so that is that now Last thing I want to show you Because this would be pretty boring if it was always the same word So What we can do here Is we're gonna use something called the random module. So I'm gonna go ahead import random And what I'm gonna do watch this let's take a couple lines I want to make a new list Oh, we have done this. So I want to make a list of words Notice the square brackets In quotation marks. So ubuntu Okay My touch pads track pads very sensitive. I'm gonna turn that off use my mouse. Um, so I'm gonna add ubuntu say python Tokyo ed tech and you can add as many words as you like it doesn't matter notice It's quotation marks comma quotation marks comma ed tech and what else can we add here ubuntu and genie But you can add your own words. So what we do here Is actually pretty cool. Notice we imported random. So we go ahead and do random dot choice Words and what that does is it chooses a random word from this list Now the problem is now that guess Is six it's always six Now that'll work for python because it's also six, but it will not work for tokyo ed tech and it won't work for genie so What we do And this is a python thing, which is actually pretty cool. We can actually multiply by length of words Uh, we're like length of word, excuse me So that will that will give So this has six this will give us six dashes This is six. It'll give us six dashes one two three four five six seven eight nine ten eleven It'll give us 11 dashes Okay, so let's go ahead and try this And so here's the word now. We know what the word is going to be because it only has five dashes But somebody, you know running your program might not know that so let's go ahead and try a let's do l That's the most common letter. Oh and yes Oh, that isn't okay l n r s ooh t good See a yes Now, of course, we know it's genie. Uh, let's go ahead and see if we can get the whole thing working Okay, you win and you live to play another day what I might do actually what I might do in that situation is I might copy this guy here And let's see. Where's that? Where's that code at? And before I exit that print again, the three quotation marks notice how the indenting is is Not concerned here between the three quotation marks So we'll let that person live And then put the three quotation marks and So this person looks really happy. So let's go ahead and run it. Yeah, we got to test it. Okay, so l n r s p Oh Oh, what's this one? Oh boob into Duh, and let's go ahead and win it And now my player looks Really happy you live you win and you live to play another day. Okay, so that is how you do a very simple hangman game. Okay, so hopefully That was follow. Well, again, the really complicated part is this whole section here where you're comparing each letter to the letter You know to the letter in the temp or in the temp Well, you're adding to the temp and comparing each letter to the gas and all that kind of stuff So, you know watch that section a couple times and hopefully it'll be clear to you how it works If not, you can always just copy this and try to figure it out a little bit later And then um, you know, you check for a winner And then we print the hangman and then check for a loser. So That is that So thanks for watching. I'll put links below to this code and uh to some other things and stuff about genie And again, please subscribe click the bell if you like what you see and want to learn more Okay, have a great day and keep on coding