Hangman in Python (5/6): winning, losing and polish
Write clear win and loss messages, reveal the secret word only on a loss, track simple session statistics, and load optional themes from several files.
Getting started
Home Assistant
Buying
Write clear win and loss messages, reveal the secret word only on a loss, track simple session statistics, and load optional themes from several files.


AI-generated illustration
Some links on this page are affiliate links. The button opens Amazon so you can check today’s price.
The loop works. You can polish the player experience and add small statistics so the game feels more satisfying.

print(f"The word was: {secret}")).Avoid revealing the word along the way if you want to keep the tension.
Global variables, or a dictionary stats = {"parties": 0, "gagnees": 0}, incremented after each game. Print a summary when the user quits (q on the menu).
Offer a menu: 1 = animals, 2 = computing → paths mots_animaux.txt, mots_info.txt. Same charger_mots function, a different path argument.
import time: record t0 = time.time() at the start of the game and print the duration at the end.
gagnees / parties * 100, with a guard if parties == 0).stats_pendu.txt (one line per session, or a running total — your choice).Part 6 — Assemble the project, file layout and extensions.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Hangman — Chapter 5/6: session stats and end messages.
Run: python pendu_chapitre_05.py
"""
import random
from pathlib import Path
BASE = Path(__file__).resolve().parent
CHEMIN_MOTS = BASE / "mots_pendu.txt"
PENDU_ETAPES = [
"",
" O ",
" O\n | ",
" O\n/| ",
" O\n/|\\\n",
" O\n/|\\\n/",
" O\n/|\\\n/ \\",
]
VIES_MAX = 7
def charger_mots(chemin):
with open(chemin, "r", encoding="utf-8") as f:
lignes = f.read().splitlines()
mots = [L.strip().upper() for L in lignes if L.strip()]
filtres = [m for m in mots if 4 <= len(m) <= 8]
if filtres:
mots = filtres
if not mots:
raise ValueError("No words in the file.")
return mots
def afficher_etat(masque, vies, essayees):
erreurs = VIES_MAX - vies
etape = min(erreurs, len(PENDU_ETAPES) - 1)
if PENDU_ETAPES[etape]:
print(PENDU_ETAPES[etape])
print("\n" + " ".join(masque))
print(f"Lives: {vies}")
print("Letters guessed:", " ".join(sorted(essayees)))
def demander_lettre_valide(essayees):
while True:
c = input("Letter? ").strip().upper()
if len(c) != 1 or not c.isalpha():
print("One letter only, A-Z.")
continue
if c in essayees:
print("Already guessed.")
continue
return c
def jouer_une_partie(secret, stats):
masque = ["_"] * len(secret)
essayees = set()
vies = VIES_MAX
coups = 0
stats["parties"] += 1
while True:
afficher_etat(masque, vies, essayees)
if "_" not in masque:
print("Well done — word found!")
stats["gagnees"] += 1
print(f"Turns played: {coups} | Mistakes: {VIES_MAX - vies}")
return
if vies <= 0:
print(f"You lose! The word was: {secret}")
print(f"Turns played: {coups}")
return
lettre = demander_lettre_valide(essayees)
essayees.add(lettre)
coups += 1
if lettre not in secret:
vies -= 1
else:
for i, L in enumerate(secret):
if L == lettre:
masque[i] = lettre
def main():
stats = {"parties": 0, "gagnees": 0}
try:
mots = charger_mots(CHEMIN_MOTS)
except FileNotFoundError:
print("Put mots_pendu.txt next to the script.")
return
while True:
secret = random.choice(mots)
jouer_une_partie(secret, stats)
if input("\nPlay again? (y/n) ").strip().lower() not in ("o", "oui", "y", "yes"):
break
p = stats["parties"]
g = stats["gagnees"]
pct = (100 * g / p) if p else 0.0
print(f"\nSession: {g} win(s) out of {p} game(s) ({pct:.0f}%).")
if __name__ == "__main__":
main()
pendu_chapitre_05.py — with mots_pendu.txt.
A book search for clean Python practice: Check today’s price on Amazon.
Was this page helpful?
Thank you!
What should we improve?
Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.
Cookies and analytics
We use Google Analytics 4 to understand how the site is used (page views, navigation). It is only switched on if you accept. Declining does not stop you using the site. More: cookie settings and privacy policy (in French).