Executioner game in Python

Counters | Python Methods and Functions

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".

Implementation

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!' )

continue

 

  # Check if the assumption is correct

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 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 & lt; = 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 P37_HangmanGame.py 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 contribute.python.engineering 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.





Tutorials