Hangman Game- Python Code

Hangman Game is a letter guessing game.

In the game of Hangman, a clue word is given by the program that the player has to guess, letter by letter. The player guesses one letter at a time until the entire word has been guessed. (In the actual game, the player can only guess 6 letters incorrectly before losing).

Let’s say the word the player has to guess is “EVAPORATE”. For this exercise, write the logic that asks a player to guess a letter and displays letters in the clue word that were guessed correctly. For now, let the player guess an infinite number of times until they get the entire word. As a bonus, keep track of the letters the player guessed and display a different message if the player tries to guess that letter again. Remember to stop the game when all the letters have been guessed correctly! Don’t worry about choosing a word randomly or keeping track of the number of guesses the player has remaining – we will deal with those in a future exercise.

An example of interaction can look like this:

>>> Welcome to Hangman!
_ _ _ _ _ _ _ _ _
>>> Guess your letter: S
Incorrect!
>>> Guess your letter: E
E _ _ _ _ _ _ _ E
...

And so on, until the player gets the word. Here is the source code to do the same in Python

#   Hangman Games
#   program by          : rakesh kumar
#   website             : https://cbsetoday.com


import random as r

# steps to select a random word from sowpods.txt file
with open("sowpods.txt") as file:
    data = file.read().split()
    number = r.randint(0, len(data))
    word = data[number]

# create a list of underscore -
# it contains exactly same number of underscore as the length of our random word
lines = []
for i in word:
    lines.append('_')

#   counter variable is used to track number of wrong letters.
#   a user can make If it is 6 then terminate the program and print message

counter = 0
while True:
    letter = input('\nGuess your letter :')
    if letter in word:
        for i in range(0, len(word)):
            if word[i] == letter:
                lines[i] = letter
    else:  # letter is not in the word
        counter += 1
    # print the word with inserted letters
    for i in lines:
        print(i, end=" ")

    # check letters remained in the list

    cnt = "".join(lines).count('_')

    if cnt == 0 or counter == 6:
        break
# end of while loop

if counter >= 6:
    print("\n\n\n You looser..............Think properly")
else:
    print("\n\n\n Yes!!!!!!!!!!! You WON this match")

In this hangman program, we are actually taking a random word from a text file “sowpods.txt” and then using the same word designed the whole program. You can also do the same with a list of words.

Well this is my version of hangman letter guessing game, What is your way to solving this problem

Related Posts

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.