1 The Problem
We want the classic hangman game: the computer picks a word, shows it as blanks, and the player guesses letters — revealing correct ones and losing a life for wrong ones, until they win or run out. It teaches tracking several pieces of game state at once: the word, the guesses, and the lives.
2 How to Think About It
Think about what the program must remember, before any code:
3 The Build — explained part by part
Here is the complete game. Each part is explained below.
import random
words = ["python", "coding", "codex", "program"]
word = random.choice(words)
guessed = set() # letters the player has tried
lives = 6
while lives > 0:
# Build the display: revealed letters, blanks for the rest.
display = "".join(letter if letter in guessed else "_" for letter in word)
print(display)
if "_" not in display:
print("You win!")
break
guess = input("Guess a letter: ").lower()
if guess in guessed:
print("Already tried that.")
continue
guessed.add(guess)
if guess not in word:
lives -= 1
print(f"Nope. Lives left: {lives}")
else:
print(f"Out of lives! The word was {word}.")
lives = 6 — the second piece of state: how many wrong guesses are left.
display = "".join(letter if letter in guessed else "_" ...) — build what the player sees: for each letter of the word, show it if guessed, otherwise show an underscore. This rebuilds every turn from the current state.
if "_" not in display: — if there are no blanks left, every letter is revealed, so the player has won.
guessed.add(guess) — record the guess. if guess not in word: lives -= 1 — a wrong guess costs a life.
while ... else: — the
else on a loop runs only if the loop finished without break — here, that means lives ran out. A neat Python feature.
set ignores duplicates automatically.display at the top of each loop from current state..lower() on the guess.4 Test & Prove Each Part
We test the display-building logic — the trickiest part — with known words and guesses.
def make_display(word, guessed):
"""Show guessed letters, underscores for the rest."""
return "".join(c if c in guessed else "_" for c in word)
def test_all_blanks():
"""No guesses means all underscores."""
assert make_display("cat", set()) == "___"
def test_reveals_letter():
"""Guessing 'a' reveals it in 'cat'."""
assert make_display("cat", {"a"}) == "_a_"
def test_full_reveal():
"""Guessing every letter shows the word."""
assert make_display("cat", {"c", "a", "t"}) == "cat"
Run with pytest -v. The make_display function is the core logic, isolated so we can test it without playing a whole game. Isolating the tricky part is a smart testing move.
5 The Interface
What it expects
Guess a letter: pWhat it returns
p_____
Guess a letter: y
py____6 Run It & Automate It
python3 hangman.pyGuess one letter at a time before your 6 lives run out.
Jenkins runs the tests automatically — each line explained below.
______
Guess a letter: p
p_____
Guess a letter: z
Nope. Lives left: 5
p_____if "_" not in display after rebuilding it.// Jenkinsfile — automated tests on every change.
pipeline {
agent any
stages {
stage('Get the code') { steps { checkout scm } } // download the code
stage('Install') { steps { sh 'python3 -m venv venv && . venv/bin/activate && pip install pytest' } } // test tool
stage('Test') { steps { sh '. venv/bin/activate && pytest -v' } } // run every test
}
post {
success { echo 'All tests passed.' }
failure { echo 'A test failed — look above.' }
}
}
- Draw the hangman. Show ASCII art for each life lost. (Teaches: mapping state to output.)
- Categories. Let the player pick a word theme. (Teaches: organising data.)
- Two players. Let one person enter the word for another. (Teaches: hiding input.)