Hangman is a classic word-guessing game. Its origins are not exactly known but it appears to date back to Victorian times. A player writes down the first and last letters of a word, and another player guesses the letters in between.
- The program randomly selects a word from a list of secret words.
- The player has limited chances to guess the word.
- When a correct letter is guessed, it is revealed in its correct position.
- The player wins if all letters are guessed before running out of chances.
- For simplicity, the program gives word length + 2 chances.
Example: If the secret word is mango (5 letters), the player gets 7 chances.
Implementation
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(' ')
stages = [
'''
-----
| |
|
|
|
|
---------
''',
'''
-----
| |
O |
|
|
|
---------
''',
'''
-----
| |
O |
| |
|
|
---------
''',
'''
-----
| |
O |
/| |
|
|
---------
''',
'''
-----
| |
O |
/|\\ |
|
|
---------
''',
'''
-----
| |
O |
/|\\ |
/ |
|
---------
''',
'''
-----
| |
O |
/|\\ |
/ \\ |
|
---------
'''
]
word = random.choice(someWords)
if __name__ == '__main__':
print('Guess the word! HINT: word is a fruit.')
for _ in word:
print('_', end=' ')
print()
letterGuessed = ''
wrong_guesses = 0
max_chances = len(stages) - 1
flag = 0
try:
while wrong_guesses < max_chances and flag == 0:
print()
guess = input('Enter a letter to guess: ').lower()
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 already guessed that letter!')
continue
if guess in word:
letterGuessed += guess * word.count(guess)
else:
wrong_guesses += 1
print(stages[wrong_guesses])
for char in word:
if char in letterGuessed:
print(char, end=' ')
else:
print('_', end=' ')
if Counter(letterGuessed) == Counter(word):
print("\nCongratulations! You guessed the word:", word)
flag = 1
break
if wrong_guesses == max_chances:
print('\nYou lost! The word was:', word)
except KeyboardInterrupt:
print('\nGame interrupted. Bye!')
Output
Guess the word! HINT: word is a fruit.
_ _ _ _ _ _
Enter a letter to guess: m
-----
| |
O |
|
|
|
---------
_ _ _ _ _ _
Enter a letter to guess: a
-----
| |
O |
| |
|
|
---------
_ _ _ _ _ _
Enter a letter to guess: p
-----
| |
O |
/| |
|
|
---------
_ _ _ _ _ _
Enter a letter to guess: p
-----
| |
O |
/|\ |
|
|
---------
_ _ _ _ _ _
Enter a letter to guess: l
l _ _ _ _ _
Enter a letter to guess: e
l _ _ _ e e
Enter a letter to guess: y
l y _ _ e e
Enter a letter to guess: c
l y c _ e e
Enter a letter to guess: h
l y c h e e
Congratulations! You guessed the word: lychee
Explanation:
- someWords.split(' '): Converts the string of words into a list.
- stages stores different Hangman drawings for each wrong guess.
- word = random.choice(someWords): Selects a random secret word for the game.
- chances = len(word) + 2: Sets number of chances based on word length.
- wrong_guesses counts incorrect attempts.
- guess = input(...).lower(): Takes a single letter input from the player.
- if not guess.isalpha() ... Validates the input for letters only and uniqueness.
- print(stages[wrong_guesses]) displays the current Hangman stage.
- letterGuessed += guess * word.count(guess): Adds correctly guessed letters to the guessed list.
- if chances <= 0 ... Ends the game if the player runs out of chances.
Try it yourself Exercises:
- You can further enhance program by adding timer after every Guess
- You can also give hints from the beginning to make the task a bit easier for user
- You can also decrease the chance by one only if the player's guess is WRONG. If the guess is right,
player's chance is not reduced.