TechVerse Chronicles
3 min readSep 19, 2024

Here’s a more complicated game: **”Hangman”**.

In this classic word-guessing game, the player has to guess the letters of a hidden word before they run out of attempts.

### Game: **Hangman**

```python
import random

def display_hangman(tries):
stages = [ # Final state: head, torso, both arms, both legs
“””
— — — —
| |
| O
| \\|/
| |
| / \\
-
“””,
# Head, torso, both arms, one leg
“””
— — — —
| |
| O
| \\|/
| |
| /
-
“””,
# Head, torso, both arms
“””
— — — —
| |
| O
| \\|/
| |
|
-
“””,
# Head, torso, one arm
“””
— — — —
| |
| O
| \\|
| |
|
-
“””,
# Head and torso
“””
— — — —
| |
| O
| |
| |
|
-
“””,
# Head
“””
— — — —
| |
| O
|
|
|
-
“””,
# Initial state
“””
— — — —
| |
|
|
|
|
-
“””
]
return stages[tries]

def choose_word():
words = [“python”, “developer”, “hangman”, “computer”, “programming”]
return random.choice(words).lower()

def hangman():
word = choose_word()
word_letters = set(word) # Letters in the word
guessed_letters = set() # Letters guessed by the player
tries = 6

print(“Welcome to Hangman!”)

while tries > 0 and word_letters:
# Show current progress and guessed letters
word_display = [letter if letter in guessed_letters else ‘_’ for letter in word]
print(“Word: “, “ “.join(word_display))
print(f”Guessed letters: {‘, ‘.join(guessed_letters)}”)
print(display_hangman(tries))

# Get player’s guess
guess = input(“Guess a letter: “).lower()

if guess in guessed_letters:
print(“You already guessed that letter.”)
elif guess in word_letters:
guessed_letters.add(guess)
word_letters.remove(guess)
print(f”Good job! {guess} is in the word.”)
else:
guessed_letters.add(guess)
tries -= 1
print(f”Sorry, {guess} is not in the word. You have {tries} tries left.”)

print(“\n”)

if word_letters:
print(display_hangman(tries))
print(f”Game over! The word was ‘{word}’.”)
else:
print(f”Congratulations! You guessed the word ‘{word}’.”)

# Start the game
hangman()
```

### How the Game Works:

1. **Word Selection**: A random word is chosen from a list.
2. **Game Loop**:
— The player guesses letters one by one.
— If they guess a correct letter, it is revealed in the word.
— If they guess incorrectly, they lose a try, and the hangman drawing advances.
— The game shows the current state of the guessed word and any incorrect guesses.
3. **Winning and Losing**:
— The player wins if they guess all the letters before running out of tries.
— The player loses if they run out of tries before guessing the word.

### Example Output:

```
Welcome to Hangman!
Word: _ _ _ _ _ _
Guessed letters:
— — — —
| |
|
|
|
|
-
Guess a letter: p
Good job! p is in the word.

Word: p _ _ _ _ _
Guessed letters: p
— — — —
| |
|
|
|
|
-
Guess a letter: e
Good job! e is in the word.

Word: p _ _ _ e _
Guessed letters: p, e
— — — —
| |
|
|
|
|
-
Guess a letter: z
Sorry, z is not in the word. You have 5 tries left.
```

### Features:
- The game uses a visual representation of the hangman, with increasing detail as wrong guesses are made.
- Players can see their progress on the word and the letters they’ve guessed.
- The game ends when the player either guesses the word or runs out of attempts.

This is a fun game that also helps practice conditional statements, loops, and string manipulations in Python!

TechVerse Chronicles
TechVerse Chronicles

Written by TechVerse Chronicles

Python learner, experienced banker, dedicated father, and loving son, on a journey of growth and discovery. Passionate about coding and family life.

No responses yet