Hangman Game in Python

Posted in /  

Hangman Game in Python

Vinay Khatri
Last updated on July 21, 2024

    Here in this program, we will code for the Hangman game in Python. Hangman is a game in which the computer provides a number of blank spaces to the user to fill those blank spaces with letters. Apparently, Hangman is a paper and pencil guessing game like tic-tac-toe. The game can be played between two or more than two persons, in this game One player thinks of a word, and the other tries to guess it by suggesting letters. Here the player gets 2x numbers of chances where x is the total length of the word to guess.

    Hangman Game in Python

    Steps to follow

    • Here the computer will think a word and the user has to guess it.
    • First, we let the user know the total length of the word that the computer has picked to guess
    • Then we ask the user to enter an appropriate letter.
    • The user will get only twice the changes as the length of the word, if the word is of 3 letters then the user will only get 6 chances to enter the letter.
    • After each guess will the program will tell the user whether the entered letter is right or wrong.
    • If the user guesses the word correctly before he loses his all chances then the user will win the game.

    Function using in the Program

    Function Name Description
    hangman_game(string) this function will act as the main function of the program and contain all the logics and functions, this function accepts an argument ‘ word’ which is a random word chosen by the computer to and the user have to guess the word.
    select_word() this function will randomly select a word that will pass as an argument to the hangman_game().
    guessed_word(string,list) this function accepts the word as a string and the user guessed letter as a list and add the guessed letter in the blanks if the letter presents in the word
    available_letters(list) this function accepts the list of guessed letters and tells you which letters you can guess now. This function helps the user to track which letter he can guess.
    is_gussed(string, list) This function accepts the word as a string and the guessed letters as a list and calculates whether the entered letter had been already guessed or not.
    import random
    def select_word():
        words_in_computer_memory = ['magazine','stars','computer','python','organisation']
        word = random.choice(words_in_computer_memory)
        return word
    def is_gussed(word, guessed_letter_list):
        for letters in word:
            if letters in guessed_letter_list:
        if count==len(word):
            return True
            return False
    def guessed_word(word, guessed_letter_list): 
        for key in word:
            if key in guessed_letter_list:
                string+="_ "
        return string
    def available_letters(guessed_letter_list):
        for letter in s:
            if letter in guessed_letter_list:
        return string
    def hangman_game(word):  
        print('''------------------WELCOME TO HANGMAN GAME---------------------------
                                       / \  
        print("The word you have to guess is of ",length, "letters long.")
        while (chances!=0):     
            if word!=guessed_word(word, guessed_letter_list):
                print("You Got", chances, "Chances.")
                print("Letters you can enter should be from these ",available_letters(guessed_letter_list))
                guess=input("ENTER A LETTER ")
                guessInLowerCase = guess[0].lower()      
                if guessInLowerCase  in guessed_letter_list:
                    print("SORRY! YOU HAVE GUSSED THIS LETTER ALREADY! ",guessed_word(word, guessed_letter_list))
                elif guessInLowerCase not in word: 
                    print(" SORRY! THE LETTER IS NOT IN WORD",guessed_word(word, guessed_letter_list))
                    print("NICE YOU GUSESSED THE RIGHT LETTER! ",guessed_word(word, guessed_letter_list))
            elif word==guessed_word(word, guessed_letter_list):
                print("YOU WON!")
       YOU LOSS!!
                                    / \  
            print('The word was',word,)
    word = select_word()


    Developing the Hangman game in Python is pretty easy since Python provides various functions to facilitate code writing. In this tutorial, we have highlighted the functions required to create the game, along with their descriptions in a table. In addition, we have listed out steps on how to start and proceed further to develop the Hangman game. If you have any queries regarding this article, you can post them in the comments section below.

    People are also reading:

    Leave a Comment on this Post