# Hangman Game in Python

Hangman Wiki:
The origins of Hangman are obscure meaning not discovered, but it seems to have arisen in Victorian times, ” says Tony Augarde, author of The Oxford Guide to Word Games. The game is mentioned in Alice Bertha Gomme’s “Traditional Games” in 1894 under the name “Birds, Beasts and Fishes.” The rules are simple; a player writes down the first and last letters of a word and another player guesses the letters in between. In other sources, [where?] the game is called “Gallows”, “The Game of Hangin”, or “Hanger”.

Implementation

This is a simple Hangman game using Python programming language. Beginners can use this as a small project to boost their programming skills and understanding logic.

1. The Hangman program randomly selects a secret word from a list of secret words. The random module will provide this ability, so line 1 in program imports it.
2. The Game: Here, a random word (a fruit name) is picked up from our collection and the player gets limited chances to win the game.
3. When a letter in that word is guessed correctly, that letter position in the word is made visible. In this way, all letters of the word are to be guessed before all the chances are over.
4. For convenience, we have given length of word + 2 chances. For example, word to be guessed is mango, then user gets 5 + 2 = 7 chances, as mango is a five letter word.
```

[code]
# Python Program to illustrate
# Hangman Game
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(' ')
#randomly choose a secret word from our &quot;someWords&quot; #list.
word = random.choice(someWords)

if __name__ == '__main__':
print('Guess the word! HINT: word is a name of a fruit')

for i in word:
# For printing the empty spaces for letters of the word
print('_', end = ' ')
print()

playing = True
# list for storing the letters guessed by the player
letterGuessed = ''
chances = len(word) + 2
correct = 0

try:
while (chances != 0):
print()
chances -= 1

try:
guess = str(input('Enter a letter to guess: '))
except:
print('Enter only a letter!')
continue

# Validation of the guess
if not guess.isalpha():
print('Enter only a LETTER')
continue
elif len(guess) &gt; 1:
print('Enter only a SINGLE letter')
continue
elif guess in letterGuessed:
print('You have already guessed that letter')
continue

# If letter is guessed correcly
if guess in word:
letterGuessed += guess

# Print the word
for char in word:
if char in letterGuessed:
print(char, end = ' ')
correct += 1
else:
print('_', end = ' ')

# If user has guessed all the letters
if (Counter(letterGuessed) == Counter(word)):
print()
print('Congratulations, You won!')
break

# If user has used all of his chances
if chances == 0:
print()
print('You lost! Try again..')
print('The word was {}'.format(word))

except KeyboardInterrupt:
print()
print('Bye! Try again.')
exit()

# print(letterGuessed)
[/code]

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182

[code]# Python Program to illustrate  # Hangman Gameimport 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(' ')#randomly choose a secret word from our &quot;someWords&quot; #list.word = random.choice(someWords) if __name__ == '__main__':     print('Guess the word! HINT: word is a name of a fruit')  for i in word:     # For printing the empty spaces for letters of the word     print('_', end = ' ')        print()  playing = True# list for storing the letters guessed by the player letterGuessed = ''               chances = len(word) + 2correct = 0 try:     while (chances != 0):         print()         chances -= 1         try:             guess = str(input('Enter a letter to guess: '))         except:             print('Enter only a letter!')             continue         # Validation of the guess         if not guess.isalpha():             print('Enter only a LETTER')             continue        elif len(guess) &gt; 1:             print('Enter only a SINGLE letter')             continue        elif guess in letterGuessed:             print('You have already guessed that letter')             continue          # If letter is guessed correcly         if guess in word:             letterGuessed += guess          # Print the word         for char in word:             if char in letterGuessed:                 print(char, end = ' ')                 correct += 1            else:                 print('_', end = ' ')          # If user has guessed all the letters         if (Counter(letterGuessed) == Counter(word)):             print()             print('Congratulations, You won!')             break     # If user has used all of his chances     if chances == 0:         print()         print('You lost! Try again..')         print('The word was {}'.format(word))  except KeyboardInterrupt:     print()     print('Bye! Try again.')     exit()      # print(letterGuessed) [/code]

```