Change language

Executioner game in Python


Hangman Wiki:
The origins of Hangman are unclear, their meaning has not been discovered, but it seems to have originated in Victorian times ", — says Tony Augard, author of the Oxford Handbook of Word Games. The game is mentioned in Alice Bertha Gomm’s Traditional Games in 1894 under the title Birds, Beasts and Fish. The rules are simple; the player writes down the first and last letters of the word, and the other player guesses the letters between them. Other sources [where?] The game is called "Hangman", "Hangin Game" or "Hanger".


It’s simple Hangman game using Python programming language. Newbies can use this as a small project to improve their programming skills and understand logic.

  1. The Hangman program randomly selects a secret word from a list of secret words. The random module will provide this opportunity, so line 1 in the program will import it.
  2. Game: here a random word (fruit name) is selected from our collection, and the player has a limited chance of winning the game.
  3. When the letter in this word is guessed correctly, the position of the letter in the word becomes visible. Thus, all letters of the word must be guessed before all chances run out.
  4. For convenience, we have given the word length + 2 chances. For example, the word to guess is — this is a mango, then the user gets 5 + 2 = 7 chances, since mango — it’s a five-letter word.

# Python program for illustration
# Executioner

import random

from collections import Counter


someWords = & # 39; & # 39; & # 39; apple, banana, mango, strawberry

orange grapes pineapple apricot lemon coconut watermelon
cherry papaya berry peach lychee musk melon & # 39; & # 39; & # 39;


someWords = someWords.split ( ’’ )

# randomly choose a secret word from our LIST someWords.

word = random.choice (someWords) 


if __ name__ = = ’__main__’ :

print ( ’Guess the word ! HINT: word is a name of a fruit’ )


  for i in word:

# To print blank spaces for the letters of the word

print ( ’_’ , end = ’ ’

  print ()


  playing = True

# list for storing the letters the player has guessed

  letterGuessed = ’’ 

chances = len (word) + 2

correct = 0

flag = 0

try :

while (chances! = 0 ) and flag = = 0 : # flag is updated when the word is correctly guessed

print ()

chances - = 1


try :

  guess = str ( input ( ’Enter a letter to guess:’ ))

  except :

print ( ’Enter only a letter!’ )



  # Check if the assumption is correct

if not guess.isalpha ():

print ( ’Enter only a LETTER’ )


elif len (guess)"  1 :

print ( ’Enter only a SINGLE letter’ )


elif guess in letterGuessed:

print ( ’You have already guessed that letter’ )




# If the message is guessed correct

if guess in word:

k = word.count (guess) #k stores the number of times a guessed letter appears in a word

for _ in range (k): 

letterGuessed + = guess # The guess is added as many times as it occurs


 < / code> # Print word

for char in word:

if char in letterGuessed and (Counter (letterGuessed)! = Counter (word)):

  print (char, end = ’ ’ )

correct + = 1

# If user guessed all letters

elif (Counter (letterGuessed) = = Counter (word)): # Once the correct word is fully guessed,

# game ends even if chance remains

print ( "The word is:" , end = ’’ )

print (word)

  flag = 1

print ( ’Congratulations, You won!’ )

break # To break out of the for loop

break # To break out of the loop

else :

print ( ’_’ , end = ’’ )




  # If the user has taken every chance

if chances " = 0 and (Counter (letterGuessed)! = Counter (word)):

print ()

print ( ’You lost! Try again..’ )

print ( ’The word was {}’ . format (word))


except KeyboardInterrupt:

print ()

print ( ’Bye! Try again.’ )

exit ()

Note: please , run the program in your terminal.

 omkarpatha k @ omkarpathak-Inspiron-3542: ~ / Documents / Python-Programs $ python Guess the word! HINT: word is a name of a fruit _ _ _ _ _ Enter a letter to guess: m _ _ m _ _ Enter a letter to guess: o _ _ mo _ Enter a letter to guess: ll _ mo _ Enter a letter to guess: elemo _ Enter a letter to guess: nlemon Congratulations, You won! 

Try it Yourself Exercises:

  • You can further improve the program by adding a timer after each Guess
  • You can also give hints from the beginning to make the task a little easier for the user
  • You can also reduce the chance by one only if the player’s guess is WRONG. If the assumption is correct,
    the player’s chance is not reduced

This article is provided by Omkar Pathak . If you are as Python.Engineering and would like to contribute, you can also write an article using or by posting the article [email protected] ... See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.


Learn programming in R: courses


Best Python online courses for 2022


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Latest questions


Common xlabel/ylabel for matplotlib subplots

12 answers


How to specify multiple return types using type-hints

12 answers


Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers


Flake8: Ignore specific warning for entire file

12 answers


glob exclude pattern

12 answers


How to avoid HTTP error 429 (Too Many Requests) python

12 answers


Python CSV error: line contains NULL byte

12 answers


csv.Error: iterator should return strings, not bytes

12 answers



Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python


How to specify multiple return types using type-hints


Printing words vertically in Python


Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries


Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically