Hangman Game in Python
Hangman is a word-guessing game where a player tries to guess a word by suggesting letters. For each incorrect guess, a part of the hangman is drawn. The game ends when the player correctly guesses the word or the hangman is complete.
Python Code to Create Hangman Game
Let's start with a basic structure for our Hangman game −
import random
def get_word(word_list):
# Function to randomly select a word from the list word = random.choice(word_list)
return word.upper()
def display_hangman(tries):
# Function to display the hangman based on the number of tries
stages = [ '''
------
| |
|
|
|
|
=========''',
'''
------
| |
| O
|
|
|
=========''',
'''
--------
| |
| O
| |
|
|
=========''',
'''
-------
| |
| O
| /|
|
|
=========''',
'''
-------
| |
| O
| /|\\
|
|
=========''',
'''
--------
| |
| O
| /|\\
| |
|
=========''',
'''
------
| |
| O
| /|\\
| |
| /
=========''',
'''
--------
| |
| O
| /|\\
| |
| / \\
========='''
]
return stages[tries]
def play_hangman():
word_list = ["PYTHON", "HANGMAN", "GUESS", "CODE"]
word = get_word(word_list)
word_letters = set(word) # Letters in the word alphabet = set('ABCDEFGHIJKLMNOPRSTUVWXYZ')
used_letters = set() # What user has guessed
lives = 6
# Getting user input
while len(word_letters) > 0 and lives > 0:
print(display_hangman(lives))
word_list_display = [letter if letter in used_letters else '_' for letter in word]
print('Current word: ', ' '.join(word_list_display))
user_letter = input('Guess a letter: ').upper()
if len(user_letter) != 1:
print("\nPlease enter a single letter.")
elif user_letter in alphabet - used_letters:
used_letters.add(user_letter)
if user_letter in word_letters:
word_letters.remove(user_letter)
else:
lives -= 1
print('\nLetter is not in the word.')
elif user_letter in used_letters:
print('\nYou have already guessed that letter.')
else:
print('\nInvalid character.')
# Game over
if lives == 0:
print(display_hangman(lives))
print('You died. The word was', word)
else:
print('You guessed the word!')
play_hangman()
Output
| | | O | /|\ | | | / ========= Current word: _ _ _ _ Guess a letter: a Letter is not in the word.
Output
| | | O | /|\ | | | ========= Current word: _ _ _ _ Guess a letter: s Letter is not in the word. | | | O | /|\ | | ========= Current word: _ _ _ _ Guess a letter: a You have already guessed that letter. | | | O | /|\ | | ========= Current word: _ _ _ _ Guess a letter: e | | | O | /|\ | | ========= Current word: _ _ _ E Guess a letter: n Letter is not in the word. | | | O | /| | | ========= Current word: _ _ _ E Guess a letter: w Letter is not in the word. | | | O | | | | ========= Current word: _ _ _ E Guess a letter: d | | | O | | | | ========= Current word: _ _ D E Guess a letter: m Letter is not in the word. | | | O | | | ========= Current word: _ _ D E Guess a letter: p Letter is not in the word. | | | | | | ========= You died. The word was CODE
Code Explanation
- Import random Imports the random module for selecting a random
- get_word function Selects a random word from the provided word list and returns it in
- display_hangman function Displays the hangman based on the number of incorrect
- play_hangman function
- Creates a word list, selects a random word, and initializes sets for word letters, alphabet, and used
- Sets the number of lives to
- The game loop continues until all letters are guessed or lives reach
- Displays the hangman, current word, and prompts for user
- Checks if the guessed letter is correct, incorrect, or already
- Updates lives and displays messages accordingly.
After the game ends, displays the final result.
python_projects_from_basic_to_advanced.htm
Advertisements