Python for Beginners: Build a Fun Hangman Game – Step-by-Step Coding Practice | Navid Ansari | Skillshare

Playback Speed


1.0x


  • 0.5x
  • 0.75x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 1.75x
  • 2x

Python for Beginners: Build a Fun Hangman Game – Step-by-Step Coding Practice

teacher avatar Navid Ansari

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

    • 1.

      01 Hangman introduction

      1:08

    • 2.

      02 Random word

      2:20

    • 3.

      03 placeholder for secret word

    • 4.

      04 Take a guess

      1:42

    • 5.

      05 Change the display word if guess is correct

      3:15

    • 6.

      06 Use range

      3:12

    • 7.

      07 Dont let number or more than one letter at time

      2:32

    • 8.

      08 Check guessed letters

      3:47

    • 9.

      09 Count incorrect guesses

      4:17

    • 10.

      10 Win situation

      1:32

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

1

Student

--

Projects

About This Class

Want to learn Python in a fun, practical way? In this beginner-friendly course, you’ll build your own Hangman Game from scratch while mastering core Python concepts like loops, lists, conditionals, and user input handling.

By the end, you’ll:
 Code a fully functional Hangman game in Python (just like the classic word-guessing challenge!).
 Understand essential programming concepts like strings, lists, loops, and functions.
 Debug errors confidently and improve your problem-solving skills.
 Gain hands-on practice with a project you can customize and expand.

Perfect for:

  • Absolute beginners who want real coding practice (not just theory!).

  • Aspiring developers looking for a small, fun project to build confidence.

  • Teachers or parents who want a simple yet engaging Python lesson.

No prior coding experience? No problem! We’ll walk through every line of code together, so you finish with a working game—and the skills to create more.

Enroll now and start coding in minutes! 

Meet Your Teacher

Teacher Profile Image

Navid Ansari

Teacher

Hello, I'm Navid.

See full profile

Level: Beginner

Class Ratings

Expectations Met?
    Exceeded!
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. 01 Hangman introduction: For this Python practice, we want to create a hangman game. So our application we select a secret word, and users should guess that. What is that secret word? And how they done it, for example, they put N in their head inter and we tell them if there is N inside the secret word. If there is, we will show that where in our secret word is that letter, and they have a limitation as well. They can guess, for example, six time, right? After that, they know, for example, N is inside the secret word. After that, they will put another letter. And again, we check if that new letter is inside the secret word or not. At the end, if they guess the secret word correctly in less attempts, for example, six time, they will win. But if they can't guess the letter after six or seven attempts, they will lose. That's what we want to create in this practice. 2. 02 Random word: No, let's just create our hangman game project. For doing that, I want to go to desktop in here, right click, create a new folder, and I want to call it hang man, right? And let me put it over here. No, go to VS code, go to here and open that folder up. It is in desktop. It's called Hangman, Head Select Folder, and that's it. In here, I want to create a new file and I want to call it hangman dot PY, right? Now, let me zoom a little bit. Knowing here what I want to do, I want a list of some words, so we randomly get one of them each time and tell the player to guess it, right? So for doing that, I will create a variable. I will call it words. I is equal to a list. And let's just add some fruits name. Apple, for example, after that banana, after that orange. After that grape, after that watermelon, right? And that's it. That's more than enough. Now, we want to randomly get one of them. And how we can do that, we can use random library. So in here, what I want to do I want to say import Random, right? And with this library, we can say random dot choice. So it will randomly choose one of these words. You can pass the words there. If you hover over the choice, it will choose a random element, right, and it will give it as return, and we can save it somewhere. I want to call it secret Word is equal to this, right? Now, with that, let's just show it. Print secret word. And with that, I want to go here, go to terminal and open up new terminal. In here, I will say PY head tap. I will put dang man there because it's just hangman in there, head inter. Now, you can see it did choose orange. Again, let's just run it one more time. No, it did choose watermelon. So no, whenever we run our application, it will choose a random words from this list of words. 3. 03 placeholder for secret word: No, we want to give the player a little bit of hint, right? So for doing that, first, we are selecting a secret word, right? After that, we don't want to show the secret word. We want to show some, for example, re scores to tell them the letter that you want to guess is, for example, five letter, right? And I want to use rescore for that. So for doing that in here, I want to create a variable, I will call it display. Word is equal to a list. And no only use a four loop, and for each letter inside secret word, we want to add underscore to display word. So we say display word dot append. You can add to it with append a underscore, right? A string of underscore. And know with that done if you print this, let's see what will happen. Print. And here, show the display word. Control is to save. Now, in here. Let's run, PY hangman dot PY headinter. You can see the secret word. This time is Apple. Apple is one, two, three, four, five, and now in the display word, we have one, two, three, four, five, right? Cool. No, I don't want to show it as a list. I want to show it in a better way. I want to join all of these rescores together as a string. How we can do that. For doing that in here, I want to create another variable I will call it display word, STR for a string. It is equal to empty string. And now with that, I will say display wordtr dot join. This join can get a list, and it can concatenate all the characters inside that list together and give us a string, right? So in here, we want to join the display word, right? But this join is giving us something in return. It will give us a string, right? So we should save it somewhere. So in here, we can say A is equal to this, right, and show the A after that. Control is to save. No, let's just run, and now you can see Apple. And instead of that, we have Or score a string of Andrei scores. Let's just run it one more time. It's orange, and you can see the rescores there. Again, let's just run. Again, orange. Let's just run grape, run, watermelon. You can see it's working fine. No with that done, I don't want to just show it. What I want to do. I want to use F string like this, and I want to say current. Word is an open closed bracket and show the A, right? This way looks much more better, right? Bushes run. You can see banana, current word is this, right? Co. This way is much better to work with. No, I don't want to save that inside the variable. I can just delete that and use all of these control s to cut it and paste it over here directly. This way is more efficient, right? But run, it will be the same. Grape, the current word is these underscores, right? 4. 04 Take a guess: No, it's time for the player to take a guess, right? And it should happen again and again, right? So for doing that, we need a le loop. So in here, I will say, will, true. So first thing, inside this y loop, we want to show display word. Control to cut, and in here, paste it. Make sure you end it both of two lines to be at the same line. No, with that done, we need to have a input, right? We get the letter on the input. So we say input. After that, take a guess, right? And this take guess will give us the letter that player write in console. But what I want to do with that, I want to make it in lower case. So in here, I can just say dot lower, right? With this method, it always give us in a lower case. So with that, we can save it inside a variable. Let's just call it guess is equal to this. We want to check if the guess is inside the secret word or not. So for that, we say I guess is in secret word, print, correct guess. That's it. So just with that, let's just run to see what will happen. We know that the secret word is grape, right? So, no, if we put, for example, Tinder, let's see what will happen. It won't say you guess correctly, right? But if we say R, for example, and head inter, it says, correct guess. So no, we are checking if the player guessing correctly or not. 5. 05 Change the display word if guess is correct: Know that we know if they take a correct guess, we need to first see where in our secret word that guess was and get that location. The index of that location, that guest letter is and change the display word in that location to the guest letter. And for doing that, I want to use a for loop. So in here, I will show you two ways of doing it because I want you to learn something in here. So in here, I want to create a variable, I will call it index is equal zero because we want to loop through our secret word. That's why we need to find the index of the correct guess, right? So we say four letter in secret word, right? In there, we check I letter. Was equal to the guess, right? In that case, we need to change the display word on that index, exact index to the guess, right? And after this, what we want to do each time we want to increase the index, right? Index plus equal to one, right? Now what will happen. Index is zero, right? So these four letter in secret word will give us the first letter. For example, let's say Apple, right? We have A at the first index and we guess P, for example, right? So A is not equal to guess, so it will go to the next index and index no is one, it will get the next letter, that is P. We guess P. So if letter that is P with the guess that is P as well is equal to each other and index is one right now, we change the display word on index that is one to the guess, that is P, right. And after that, we increase the index again. Now, we go to the next letter, that is again a P, right? And again, we check P is equal to the guess that we guess P. So again, the display word for index two, will be P as well, and it will be changed. After that, we go to the next one that is L in Apple, right? And L is not equal to P. So that's why this won't happen, but the index will be increased. And with this, we are checking all the letters inside secret word, right? So in here, let's just run it. The banana, right? If you put B in the head inter, you can see. Now, the display word will show B at the beginning, Index zero. If you put N in there, you can see it will search through banana. You can see on index zero, one, two, we have N, and 34 on index four, we have N as well. And in here, we can show it, right? Next, let's just go A, and you can see the banana is finished and we guessed it correctly. 6. 06 Use range: No, I want to show you another way of doing the exact same thing that we have done in here, so you will learn some new things. In here, I want to hold out and shift and drag from top to bottom and make them comment with adding hash tag there, right? No, all of them are just some comment. No, I want to do the same. I want to say I guess was in the secret word, right? In that case, again, we want to use for loop, but this time, I want to do it in more smart way. I want to say for I, I is the index. We can call it index. For index in range what this range will do, it will, for example, give us a list of numbers from zero to what we specify in here. For example, if I put four in here, it's like it's giving us a list 0-4, right? It's exactly the same thing. What we have done in here, we use a variable for that, but with this range, it will give us a list of numbers that start 0-4, for example. In here, it did start from zero to the length of the secret word. If secret word was five letter, it's like index start 0-4, right? It's exactly the same thing in here. But we don't need to create a variable anymore, and we don't want to hardcode anything here. We can just use length or length to get the length of our secret word, right? And with that, know what this will give us, it will give us the index, like it did give us in here. We create the index manually, and it will loop through all the letters of our secret word. In here, we can just say if secret word letter index, right? Index will start from zero to the length if the length is 40, one, two, three. So at the beginning, index will be zero. We get the secret word index zero. For example, if it is equal to guess, right, in that case, we can change the display word on that index on that exact index to the guess, right? Yes. That's it. You can see it's much more professional to do it this way. And it is exactly the same thing that we done in here, but it will do this index equal to zero and adding one to it all by itself, right? But just save and in here, run, and you can see it is orange. If I put N in there, you can see it is doing exactly the same thing. If I put O, if I put R, if I put G, if I put E, if I put A, and no, you can see, it's working correctly. If you put whatever, it's working fine. You can use both versions. 7. 07 Dont let number or more than one letter at time: No, we want to add some limitation. They shouldn't be able to use numbers. They shouldn't allow to put more than one character, and that's it. Let's see what we can do those, right? So when they guess, first, I want to check if length of guess was not equal to one. In that case, we want to say print. Let's say, error. Please enter one letter at time, right? After that, we shouldn't let this execution of this code to go through checking the guess is correct or not. So that's why we can use continue to tell it to go back and start over, right? With that done, let's just check it. Control C to get out of this execution and no, let's just run. If I put, for example, AS, it says, error, please enter one letter at the time. If I put more, again, it says, error, please enter one letter at time. But if I put for example, there, you can see it won't give us any error, and one of them is correct, because we put T there, right? So, no, this was the first thing that I wanted to do. Next thing, I want to check I guess OT is pa or not. If it is alphabet or not, right? And we can do it like this. If it is not equal true, right? In that case, again, we want to say print. And in there, I want to say error, please write letters. After that, we want to say continue. With this continue, it will go back and start over as well. So let's just save in here, Control, to get out of execution. Now, let's just run it again. And now, if I put number in there, it will say error, please write just letters. If I put 25, error, please enter one letter at the time. So no, let's just put eight. Again, it says error, please write just letters. If I put A D, for example, again, it says, error, please enter a letter at the time. What was the secret grape? Let's just put ginger, you can see the G will be there. No, we have some limitation. 8. 08 Check guessed letters: No next thing that you want to do. We want to save all the guests letters and don't let them to guess it again, right? So for doing that, at top in here, I want to create a list so we can save them. I will call it guest letters, right? It's equal to empty list, right? And wherever they guess doesn't matter what we can add it to this list. So we can say guest letters dot append the guess, right? Know it that done. I want to show that guest letters as well. Like we showed the display word. We want to do the same in here as well. So I can just copy and paste all of these Control C to copy this and paste it over here. Wherever we say display word, we can change it to guest letters, right STR. And I want to copy and paste this over here like this, and we want to join the guest letters. So we say guess letters. That's it. Now, with that, I want to say the guess Letters is this one, right? Let's just test that out in here. Right now, the guest letter is nothing. So let's just add one there. We don't have F, so let's just put it there. The guest letter is F. After that you can see it will be added there. After that, after that, after that, you can see, we can add things and no player can find out what they test it, right? But this way of showing it doesn't look good. It's connected together. How we can change that, we can go to guess letters SDR and add the comma there like this with in space. Now, let's just statute. Make sure you save and run. No, if I put, for example, Tinder, you can see we have After that, put Ed, you can see there is a comma after that. After that, let's just use E, E is inside. Q, and you can see that looks much better, and the errors will be there as well. So with that done, we are showing the guest letters, but we don't want to let it pass if they already guessed it, right? We don't want to check all of these if they already guessed it. So in here, before appending to the guest letters, we want to check if the guess was inside the guest letters, right? In that case, we want to say with the print you already guess this one, right? Cool. After that, you want to use it continue to tell it to go at beginning of the loop and start over because you already guess this one, right? No, let's just say start to get out of the execution and run again. No. Banana, right? Q is not there. We should put Q in there. If I put Q one more time, it says, You already guess this one, and that's working fine. If I put S, again, you can see the S will be added to guess letters, and if I put S one more time, it says, You already guess this one. If I put Q again, it will say, you already guess this one. So this way of checking if they already guessed it or not, we don't have to do all of this, right? So that was another step to make our game much better. 9. 09 Count incorrect guesses: No, we want to add another limitation for the player. They shouldn't be able to test all the letters on the keyboard, right? So we should have some limitation on that. For doing that, I want to create some variables for it, right? The first variable, I want to call it incorrect kisses. Right? It's equal to zero by default. Next, I want to call it max. Incorrect guesses, right? That for now, let's just say six. So the player can take a guess that is incorrect six time, right? Now when do we want to add to these incorrect guesses? If we go down, whenever we are checking if the guess is inside the secret word, we want to increase that incorrect guesses, right? So in here, I want to add a statement. Here, else. So if the guess is not in the secret word, what do you want to do? First, I want to use a print to say encorrectGuess, right? And after that, I want to increase the incorrect guesses by one with plus equal one, right? I know that we are increasing deep incorrect guesses, I want to show it. I want to tell them how many guesses they have left, right? So in here at top of this wire loop, I want to use a print of a string, right? So if double quotation, incorrect guesses, left is open and close curly brackets, and I want to say max incorrect guesses, subtracted by incorrect guesses, right? So if you have two incorrect guess, max incorrect guess that is six, six minus two will be four, and it will show it to us, right? Now with that down, let's just test it. In here. I want to run it again. The incorrect guesses left is six. If we put T in there because T is not in apple, it is incorrect guess, we hit inter, you can see the incorrect guesses left is five. Let's just put R in there. Again, you can see it is four edge again, you can see it is three. But if you put H again, it will stay at three. Why? Because we are increasing the incorrect guesses after we are checking if they guess it already, right? So in here, if I put Q again, you can see it will reach zero at the end. You can see, it will go minus as well. So that is a problem. We want to check. If they don't have any chance to take a guess, we want to say game over and get out of the application, right? So down in here, at the end, we want to say if incorrect guesses was bigger or equal than Max incorrect guesses. In that case, we want to print game over. And after that, I want to show the word as well. So I will say print with a FS string, right? The word was open and close curly bracket and put the secret word there, right? Control is to save. Now in here, let me run it one more time. If I put Q, you can see it will decrease. Let's just decrease it one more time. And no, let's just put Jander. It is zero. And no, if I put V, you can see it says game over, but it didn't get out of the loop. So what we can do for doing that we say break, right? That's it. No, let's just sat out. If you run one more time, put Q in there, WE, R, and some more, and you can see, take a guess. And because it was incorrect and we didn't have any more chances after that, it will say game over. The word was banana and you couldn't guess it. That was another step to create a game in Python. 10. 10 Win situation: No the last step is to check if they correctly guess the secret word or not, how we can do that. In here, before checking if the incorrect guesses is more or equal than Max incorrect guesses, we want to check if there is no underscore not in display word, right? If there is no underscore inside display word, right, in that case, it means we did guess the word correctly. So we can say print. Congratulations, right? You win. After that, let's just show the word as well. We can just copy and paste this from here to here. And after that, we can just break out of the loop because our game is finished. Now, let's just run. In here, we have six guesses to do go up a little bit so you can see everything. The word is apple. If you put in there, it will decrease the incorrect guesses to be five now. And if you put A in there, you can see the A will be here, and it will show us the guest letters as well. If you put Pinder, it will show that. If you put L in there, it will show that in here. And if you put Id, you can see it says, congratulations. You win. The word was Apple and congratulations to you for finishing this game with.