Hangman Game in Python (original) (raw)

Last Updated : 16 Jun, 2026

Hangman is a classic word-guessing game. Its origins are not exactly known but it appears to date back to Victorian times. A player writes down the first and last letters of a word and another player guesses the letters in between.

**Example: If the secret word is mango (5 letters), the player gets 7 chances.

Steps to Build the Game

  1. Create a list of words and randomly select one.
  2. Display blanks (_) for each letter in the word.
  3. Take a letter as input from the user.
  4. Check whether the letter exists in the word.
  5. Reveal correct letters and track wrong guesses.
  6. Display the Hangman drawing after each incorrect guess.
  7. End the game when the word is guessed or all chances are used.

Python Implementation

Python `

import random from collections import Counter

someWords = '''apple banana mango strawberry orange grape pineapple apricot lemon coconut watermelon cherry papaya berry peach lychee muskmelon'''

someWords = someWords.split(' ') stages = [ '''

| | | | | |

''', '''

| | O | | | |

''', '''

| | O | | | | |

''', '''

| | O | /| | | |

''', '''

| | O | /|\ | | |

''', '''

| | O | /|\ | / | |

''', '''

| | O | /|\ | / \ | |

''' ]

word = random.choice(someWords)

if name == 'main':

print('Guess the word! HINT: word is a fruit.')

for _ in word:
    print('_', end=' ')
print()

letterGuessed = ''
wrong_guesses = 0
max_chances = len(stages) - 1
flag = 0

try:
    while wrong_guesses < max_chances and flag == 0:

        print()
        guess = input('Enter a letter to guess: ').lower()

        if not guess.isalpha():
            print('Enter only a letter!')
            continue

        elif len(guess) > 1:
            print('Enter only a single letter!')
            continue

        elif guess in letterGuessed:
            print('You already guessed that letter!')
            continue

        if guess in word:
            letterGuessed += guess * word.count(guess)
        else:
            wrong_guesses += 1
            print(stages[wrong_guesses])

        for char in word:
            if char in letterGuessed:
                print(char, end=' ')
            else:
                print('_', end=' ')

        if Counter(letterGuessed) == Counter(word):
            print("\nCongratulations! You guessed the word:", word)
            flag = 1
            break

    if wrong_guesses == max_chances:
        print('\nYou lost! The word was:', word)

except KeyboardInterrupt:
    print('\nGame interrupted. Bye!')

`

**Output

Guess the word! HINT: word is a fruit.
_ _ _ _ _ _

Enter a letter to guess: m

-----
| |
O |
|
|
|
---------

_ _ _ _ _ _
Enter a letter to guess: a

-----
| |
O |
| |
|
|
---------

_ _ _ _ _ _
Enter a letter to guess: p

-----
| |
O |
/| |
|
|
---------

_ _ _ _ _ _
Enter a letter to guess: p

-----
| |
O |
/|\ |
|
|
---------

_ _ _ _ _ _
Enter a letter to guess: l
l _ _ _ _ _
Enter a letter to guess: e
l _ _ _ e e
Enter a letter to guess: y
l y _ _ e e
Enter a letter to guess: c
l y c _ e e
Enter a letter to guess: h
l y c h e e
Congratulations! You guessed the word: lychee

**Explanation:

Try it yourself Exercises: