---
title: "Python Hangman Game Program Error"  
description: "Python Hangman Game Program Error"  
author: "Gulshan Negi"  
published: 2023-05-11  
updated: 2023-07-03  
canonical: https://www.mindstick.com/forum/158289/python-hangman-game-program-error  
category: "python"  
tags: ["python", "programming language"]  
reading_time: 4 minutes  

---

# Python Hangman Game Program Error

Hello this is Gulshan Negi

Well, I am [writing](https://www.mindstick.com/articles/12997/5-essential-tips-on-resume-writing-for-business-analysts) a [program for making](https://www.mindstick.com/forum/156808/write-a-program-for-making-recursive-function-in-any-language) Hangman Game in [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python) but it [shows](https://yourviews.mindstick.com/view/81380/new-york-violent-shooting-shows-people-are-not-safe-in-night-life) some [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) at the time of its execution.

Here is my [source code](https://www.mindstick.com/forum/33476/pls-send-me-source-code-for-changing-passward-in-asp-dot-net-i-tried-so-much-from-google-but-its-not-workining):

```plaintext
import random
import time
import os

def play_again():
  question = 'Do You want to play again? y = yes, n = no \n'
  play_game = input(question)
  while play_game.lower() not in ['y', 'n']:
      play_game = input(question)

  if play_game.lower() == 'y':
      return True
  else:
      return False

def hangman(word):
  display = '_' * len(word)
  count = 0
  limit = 5
  letters = list(word)
  guessed = []
  while count < limit:
      guess = input(f'Hangman Word: {display} Enter your guess: \n').strip()
      while len(guess) == 0 or len(guess) > 1:
          print('Invalid input. Enter a single letter\n')
          guess = input(
              f'Hangman Word: {display} Enter your guess: \n').strip()

      if guess in guessed:
          print('Oops! You already tried that guess, try again!\n')
          continue

      if guess in letters:
          letters.remove(guess)
          index = word.find(guess)
          display = display[:index] + guess + display[index + 1:]

      else:
          guessed.append(guess)
          count += 1
          if count == 1:
              time.sleep(1)
              print('   _____ \n'
                    '  |      \n'
                    '  |      \n'
                    '  |      \n'
                    '  |      \n'
                    '  |      \n'
                    '  |      \n'
                    '__|__\n')
              print(f'Wrong guess: {limit - count} guesses remaining\n')

          elif count == 2:
              time.sleep(1)
              print('   _____ \n'
                    '  |     | \n'
                    '  |     | \n'
                    '  |      \n'
                    '  |      \n'
                    '  |      \n'
                    '  |      \n'
                    '__|__\n')
              print(f'Wrong guess: {limit - count} guesses remaining\n')

          elif count == 3:
              time.sleep(1)
              print('   _____ \n'
                    '  |     | \n'
                    '  |     | \n'
                    '  |     | \n'
                    '  |      \n'
                    '  |      \n'
                    '  |      \n'
                    '__|__\n')
              print(f'Wrong guess: {limit - count} guesses remaining\n')

          elif count == 4:
              time.sleep(1)
              print('   _____ \n'
                    '  |     | \n'
                    '  |     | \n'
                    '  |     | \n'
                    '  |     O \n'
                    '  |      \n'
                    '  |      \n'
                    '__|__\n')
              print(f'Wrong guess: {limit - count} guesses remaining\n')

          elif count == 5:
              time.sleep(1)
              print('   _____ \n'
                    '  |     | \n'
                    '  |     | \n'
                    '  |     | \n'
                    '  |     O \n'
                    '  |    /|\ \n'
                    '  |    / \ \n'
                    '__|__\n')
              print('Wrong guess. You\'ve been hanged!!!\n')
              print(f'The word was: {word}')

      if display == word:
          print(f'Congrats! You have guessed the word \'{word}\' correctly!')
          break

def play_hangman():
   print('\nWelcome to Hangman\n')
   name = input('Enter your name: ')
   print(f'Hello {name}! Best of Luck!')
   time.sleep(1)
   print('The game is about to start!\nLet\'s play Hangman!')
   time.sleep(1)
   os.system('cls' if os.name == 'nt' else 'clear')

   words_to_guess = [
       'january', 'border', 'image', 'film', 'promise', 'kids',
       'lungs', 'doll', 'rhyme', 'damage', 'plants', 'hello', 'world'
   ]
   play = True:
   while play:
       word = random.choice(words_to_guess)
       hangman(word)
       play = play_again()

   print('Thanks For Playing! We expect you back again!')
   exit()

if __name__ == '__main__':
  play_hangman()
```

Well, I also checked and took a [reference](https://www.mindstick.com/forum/774/reference-what-does-this-error-mean-in-php) from here. Can [anyone](https://www.mindstick.com/articles/23207/how-to-find-the-best-fit-job-for-anyone) give their suggestions on this?

Thanks

## Replies

### Reply by Gulshan Negi

Thanks a lot for your kind response. Yes, it works.

Thanks again.

### Reply by Gulshan Negi

@**Aryan Kumar** Thanks a lot for your kind response and time.

Yes, the given solution really works.

Thanks again.

### Reply by Aryan Kumar

Your code looks good. The only error I see is in the “play_again()” function. The line "play_game = input(question)" should be “play_game = input(question).lower()”. This is because the "play_again()" function is checking if the user's input is either 'y' or 'n'. If the user enters anything else, the function will keep asking the user for input until they enter a valid response.

Here is the corrected code:

```python
def play_again():
 question = 'Do You want to play again? y = yes, n = no \n'
 play_game = input(question).lower()
 while play_game not in ['y', 'n']:
     play_game = input(question)
 if play_game == 'y':
     return True
 else:
     return False
```


---

Original Source: https://www.mindstick.com/forum/158289/python-hangman-game-program-error

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
