How To Make Hangman In Python?

Hey there, Are you looking for a fun game? Then, you are on the right website, because today I’ll show you a step-by-step guide on How To Make Hangman In Python.

The coding of the Hangman Game In Python is very simple and easy to understand, which is good for beginners who want to learn Python Games.

What is Hangman Game In Python?

In a Python Hangman game, you have to guess the letters (A-Z) that make up the words. If the player guesses the right letter in the word, that letter shows up in the right place.

The person playing the game has to guess the right word until a man is hung, at which point the game is over.

How To Play Python Hangman?

To play hangman game, the system chooses a secret word, and the user tries to guess the word letter by letter. The system knows the secret and writes a string of dashes, one for each letter in the answer. At first, the only thing known about the target word is how long it is.

How To Make Hangman In Python Step-by-Step Guide For Beginners

Here’s the step-by-step guide on how to code hangman in python.

Time needed: 5 minutes

These are the steps on How To Make Hangman In Python.

  • Step 1: Import Libraries.

    First, Import all the required libraries of the python game.

    import random
    import time

  • Step 2: Create User Input To Register User Complete Name.

    Next, let’s create a user input to register the complete name of the user.

    # Initial Steps to invite in the game:
    print("\nWelcome to Hangman game by IT SOURCECODE\n")

    This line of code displays the welcome text of the game.

    name = input("Enter your name: ")
    print("Hello " + name + "! Best of Luck!")

    In these lines of code, the system asks the user to input his/her name, and after that the system print or display the name to the console.

    time.sleep(2)
    print("The game is about to start!\n Let's play Hangman!")
    time.sleep(3)

    In these lines of code, we create a time sleep function for 2 and 3 seconds and after that, the system displays the textThe game is about to start“.

  • Step 3: Create a Main Module of Game.

    Next, we will create a main module of the game named “main“.

    # The parameters we require to execute the game:
    def main():
    global count
    global display
    global word
    global already_guessed
    global length
    global play_game

    These lines of code are the global variables that use in the game to store data.

    words_to_guess = ["january","border","image","film","promise","kids","lungs","doll","rhyme","damage"
    ,"plants"]

    These lines of code are the guessing words to be guessed by the user.

    word = random.choice(words_to_guess)
    length = len(word)
    count = 0
    display = '_' * length
    already_guessed = []
    play_game = ""

    These lines of code are the variables that are assigned to its functions.

  • Step 4: Create a Loop To Ask The User If Want To Play Again.

    Next, we will create a loop to ask the user if he/she wants to play again.

    def play_loop():
    global play_game

    This line of code is the declared variable to be used in this module.

    play_game = input("Do You want to play again? y = yes, n = no \n")
    while play_game not in ["y", "n","Y","N"]:
    play_game = input("Do You want to play again? y = yes, n = no \n")
    if play_game == "y":
    main()
    elif play_game == "n":
    print("Thanks For Playing! We expect you back again!")
    exit()

    In these lines of code, it will ask the user if he/she wants to play again. If yes, the game will go to the main module if not the program will exit.

  • Step 5: Create a Hangman Module

    Lastly, we will create the actual game for hangman.

    def hangman():
    global count
    global display
    global word
    global already_guessed
    global play_game
    limit = 5
    guess = input("This is the Hangman Word: " + display + " Enter your guess: \n")
    guess = guess.strip()
    if len(guess.strip()) == 0 or len(guess.strip()) >= 2 or guess <= "9":
    print("Invalid Input, Try a letter\n")
    hangman()


    elif guess in word:
    already_guessed.extend([guess])
    index = word.find(guess)
    word = word[:index] + "_" + word[index + 1:]
    display = display[:index] + guess + display[index + 1:]
    print(display + "\n")

    elif guess in already_guessed:
    print("Try another letter.\n")

    else:
    count += 1

    if count == 1:
    time.sleep(1)
    print(" _____ \n"
    " | \n"
    " | \n"
    " | \n"
    " | \n"
    " | \n"
    " | \n"
    "__|__\n")
    print("Wrong guess. " + str(limit - count) + " guesses remaining\n")

    elif count == 2:
    time.sleep(1)
    print(" _____ \n"
    " | | \n"
    " | |\n"
    " | \n"
    " | \n"
    " | \n"
    " | \n"
    "__|__\n")
    print("Wrong guess. " + str(limit - count) + " guesses remaining\n")

    elif count == 3:
    time.sleep(1)
    print(" _____ \n"
    " | | \n"
    " | |\n"
    " | | \n"
    " | \n"
    " | \n"
    " | \n"
    "__|__\n")
    print("Wrong guess. " + str(limit - count) + " guesses remaining\n")

    elif count == 4:
    time.sleep(1)
    print(" _____ \n"
    " | | \n"
    " | |\n"
    " | | \n"
    " | O \n"
    " | \n"
    " | \n"
    "__|__\n")
    print("Wrong guess. " + str(limit - count) + " last guess remaining\n")

    elif count == 5:
    time.sleep(1)
    print(" _____ \n"
    " | | \n"
    " | |\n"
    " | | \n"
    " | O \n"
    " | /|\ \n"
    " | / \ \n"
    "__|__\n")
    print("Wrong guess. You are hanged!!!\n")
    print("The word was:",already_guessed,word)
    play_loop()

    if word == '_' * length:
    print("Congrats! You have guessed the word correctly!")
    play_loop()

    elif count != limit:
    hangman()

Download The Complete and Runnable Source Code

You can visit and download the complete and runnable source code of Hangman Game In Python for free.

Conclusion

We have completely discussed the step-by-step guide on How To Make Hangman In Python.

I hope this simple game tutorial helps you a lot enhance your knowledge of python programming.

Leave a Comment