How to make Hangman game using the Python programming language, Within 5 minutes.

0

What is Hangman Game Wiki:

The origins of Hangman are tough to grasp difficult to understand which means now not discovered, however, it seems to possess arisen in Victorian times, ” says Tony Augarde, creator of The Oxford Guide to Word Games. The recreation is declared in Alice Bertha Gomme’s “Traditional Games” in 1894 below the name “Birds, Beasts and Fishes.” the rules are simple; a participant writes down the first and supreme letters of a phrase and the other participant guesses the letters in between. in numerous sources, [where? ] the game is named “Gallows”, “The Game of Hangin”, or “Hanger”.

Implementation

This is a simple hangman game using the Python programming language by CodeWithNepal. Newbies can use it as a mini-project to improve their programming skills and understanding of logic.

Hangman randomly selects a secret word from a list of secret words. The random module will provide this capability, so line 1 of the program will import it.
Game: This is a random word (fruit name) taken from our collection and the player has a very limited chance of winning.
If the letter of the word is guessed correctly, the position of this letter in the word becomes clear. Thus, all letters in a word must be guessed before all possibilities are exhausted.

# Python Program to illustrate
# Hangman Game by CodeWithNepal
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 "someWords" 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
	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

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


			# If letter is guessed correctly
			if guess in word:
				k = word.count(guess) #k stores the number of times the guessed letter occurs in the word
				for _ in range(k):	
					letterGuessed += guess # The guess letter is added as many times as it occurs

			# Print the word
			for char in word:
				if char in letterGuessed and (Counter(letterGuessed) != Counter(word)):
					print(char, end = ' ')
					correct += 1
				# If user has guessed all the letters
				elif (Counter(letterGuessed) == Counter(word)): # Once the correct word is guessed fully,
																# the game ends, even if chances remain
					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 while loop
				else:
					print('_', end = ' ')

			

		# If user has used all of his chances
		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 on your terminal.

codewithnepal-dell:~/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
_ _ m o _ 
Enter a letter to guess: l
l _ m o _ 
Enter a letter to guess: e
l e m o _ 
Enter a letter to guess: n
l e m o n 
Congratulations, You won!

LEAVE A REPLY

Please enter your comment!
Please enter your name here