VOOZH about

URL: https://www.geeksforgeeks.org/python/hangman-game-python/

⇱ Hangman Game in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Hangman Game in Python

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.

  • Program randomly selects a word from a list of secret words.
  • Player has limited chances to guess the word.
  • When a correct letter is guessed, it is revealed in its correct position.
  • Player wins if all letters are guessed before running out of chances.
  • For simplicity, the program gives word length + 2 chances.

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

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:

  • someWords.split(' '): Converts the string of words into a list.
  • stages stores different Hangman drawings for each wrong guess.
  • word = random.choice(someWords): Selects a random secret word for the game.
  • chances = len(word) + 2: Sets number of chances based on word length.
  • wrong_guesses counts incorrect attempts.
  • guess = input(...).lower(): Takes a single letter input from the player.
  • if not guess.isalpha() ... Validates the input for letters only and uniqueness.
  • print(stages[wrong_guesses]) displays the current Hangman stage.
  • letterGuessed += guess * word.count(guess): Adds correctly guessed letters to the guessed list.
  • if chances <= 0 ... Ends the game if the player runs out of chances.

Try it yourself Exercises: 

  • You can further enhance program by adding timer after every Guess
  • You can also give hints from the beginning to make the task a bit easier for user
  • You can also decrease the chance by one only if the player's guess is WRONG. If the guess is right, 
    player's chance is not reduced.


Comment
Article Tags:
Article Tags: