Hangman in Python (3/6): display, letters and ASCII hangman
Present the masked word, remaining lives and guessed letters clearly, sort those guesses, and draw a step-by-step ASCII hangman from strings.
Getting started
Home Assistant
Buying
Present the masked word, remaining lives and guessed letters clearly, sort those guesses, and draw a step-by-step ASCII hangman from strings.


AI-generated illustration
Some links on this page are affiliate links. The button opens Amazon so you can check today’s price.
A readable terminal interface changes everything: the player sees the masked word, their lives, and the letters already played at a glance. This short article also adds a progressive ASCII hangman (optional, but motivating).
def afficher_etat(masque, vies, essayees):
print("\n" + " ".join(masque))
print(f"Lives: {vies}")
print("Letters guessed:", " ".join(sorted(essayees)))
sorted(essayees) turns the set into a sorted list for display.
Store 7 multiline strings in a list PENDU_ETAPES[0] … PENDU_ETAPES[6]: at stage k, print PENDU_ETAPES[k] where k = erreurs (from 0 to 6). You can start with a minimal drawing:
PENDU_ETAPES = [
"",
" O ",
" O\n | ",
" O\n/| ",
" O\n/|\\\n",
# ... fill in through stage 6
]
Each string can use newline characters \n. Test with print(etape).
If you start with 7 lives and one mistake removes 1 life, the drawing to show can be PENDU_ETAPES[7 - vies] (indices 0 to 6).
=== border around the game screen to separate turns.█████░░ (5 blocks out of 7).Part 4 — Main loop and input checks.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Hangman — Chapter 3/6: display and ASCII hangman (7 stages).
Run: python pendu_chapitre_03.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()]
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 main():
try:
mots = charger_mots(CHEMIN_MOTS)
except FileNotFoundError:
print("Put mots_pendu.txt next to the script.")
return
secret = random.choice(mots)
masque = ["_"] * len(secret)
essayees = set()
vies = VIES_MAX
afficher_etat(masque, vies, essayees)
print("(Demo: one display only — the loop arrives in chapter 4.)")
if __name__ == "__main__":
main()
pendu_chapitre_03.py — with mots_pendu.txt in the same folder.
A book search for Python exercises: 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).