Python Tutorial

Python Project: Hangman

Guess letters in a hidden word. Track lives, reveal matches, and stop when the word is complete.

What you are building

Hangman hides a word and accepts one letter at a time. A match reveals every copy of that letter. A miss costs a life. The game ends when the pattern has no blanks, or when lives reach zero.

Run the examples at /try in the Python editor, not HTML, C, or C++. A list of letters plays the whole game so the log prints in one Run. There is no input() in the complete program.

Skills used

  • A string for the hidden word
  • A set of guessed letters
  • A pattern function that prints letters or underscores
  • A while loop, or a for over a prepared guess list
  • A lives counter that only drops on a new miss

Reveal the pattern

The player never sees the raw word after the game starts. They see a pattern: a letter if it was guessed, an underscore if it was not. Build that string from the word and the guessed set.

Example

def pattern(word, guessed):
    parts = []
    for ch in word:
        if ch in guessed:
            parts.append(ch)
        else:
            parts.append("_")
    return " ".join(parts)

word = "python"
guessed = {"p", "t"}
print(pattern(word, guessed))
print("Lives:", 6)

That print is p _ t _ _ _ with spaces so each blank is visible. Without spaces,p____ is harder to count.

Apply one letter

A guess has three outcomes: already tried, in the word, or a miss. Only a miss reduces lives. Add every guess to the set, even misses, so a repeated miss does not punish twice.

Example

word = "python"
guessed = set()
lives = 6
letter = "z"

if letter in guessed:
    print("Already tried", letter)
else:
    guessed.add(letter)
    if letter in word:
        print(letter, "is in the word.")
    else:
        lives -= 1
        print(letter, "is a miss. Lives:", lives)

print("Guessed:", sorted(guessed))
CaseLivesPattern
New letter in the wordunchangedthat letter appears
New letter not in the wordminus oneunchanged
Repeat of an old letterunchangedunchanged
Pattern has no _winfull word
Lives reach 0lossword is revealed

Store guesses in a set. Membership tests stay fast to write: letter in guessed. When you print the tried letters, wrap with sorted(...) so the order is stable.

Complete program

The hidden word is python. The letter list is p, y, z, t, h, o, n. One miss onz, then the word fills in. Copy the whole script into /try.

Example

def pattern(word, guessed):
    parts = []
    for ch in word:
        if ch in guessed:
            parts.append(ch)
        else:
            parts.append("_")
    return " ".join(parts)

def won(word, guessed):
    return all(ch in guessed for ch in word)

WORD = "python"
LIVES = 6
LETTERS = ["p", "y", "z", "t", "h", "o", "n"]

guessed = set()
lives = LIVES

print("Hangman")
print("Word length:", len(WORD))
print("Lives:", lives)
print(pattern(WORD, guessed))

for letter in LETTERS:
    print()
    print("Guess:", letter)
    if letter in guessed:
        print("Already tried.")
        print(pattern(WORD, guessed))
        continue
    guessed.add(letter)
    if letter in WORD:
        print("Hit.")
    else:
        lives -= 1
        print("Miss. Lives left:", lives)
    print(pattern(WORD, guessed))
    print("Tried:", " ".join(sorted(guessed)))
    if won(WORD, guessed):
        print()
        print("You revealed the word:", WORD)
        break
    if lives <= 0:
        print()
        print("Out of lives. The word was:", WORD)
        break
else:
    if not won(WORD, guessed):
        print()
        print("No more guesses. The word was:", WORD)

The for/else at the bottom runs only if the loop did not break. That covers a letter list that runs out while blanks remain. This demo wins before that happens.

Common mistakes

  • Comparing letter == word. Hangman guesses letters, not the whole word, in this version.
  • Using a list for guessed and forgetting to skip duplicates. A second z would steal another life.
  • Revealing with word.replace("_", letter) on the original word. Build the pattern from the secret and the set each time. The secret string itself should not change.
  • Mixing case: secret Python and guess p. Lowercase the word once at the start, and lowercase each guess.
  • Ending the game when letter in word is true once. You win only when every character is in the guessed set.

How to extend / Practice tasks

Use the same complete program. Change the word or the letter list.

  1. Change WORD to string and write a letter list that wins with exactly one miss.
  2. Start with LIVES = 2 and a list of three misses. Confirm the game stops at zero lives and prints the secret.
  3. Reject guesses longer than one character: if len(letter) != 1, print a message and do not add it to the set. Test with "py" in the list.

📘 Real-World Deep Dive

Hangman is a great state-machine exercise: a secret word, a set of guessed letters, a dwindling life count, and a masked display recomputed after every guess. Get those four pieces right and the game just works.

What to build

The player guesses letters in a hidden word; correct letters reveal, wrong letters cost a life, and the game ends on a full reveal or zero lives.

Real-Life Example

def masked(word: str, guessed: set[str]) -> str:
    return " ".join(c if c in guessed else "_" for c in word)

def turn(word, guessed, lives, letter):
    guessed.add(letter)
    if letter not in word:
        lives -= 1
    return lives

word, guessed, lives = "python", set(), 6
for letter in "pyaton":
    lives = turn(word, guessed, lives, letter)
    print(f"{masked(word, guessed)}  lives={lives}")

Using a set for guessed letters makes "already tried?" an O(1) check and quietly ignores repeats.

Expected Output

p _ _ _ _ _  lives=6
p y _ _ _ _  lives=6
p y _ _ _ _  lives=5
p y t _ _ _  lives=5
p y t _ o _  lives=5
p y t _ o n  lives=5

Common mistakes

  • Not tracking guessed letters lets a player "lose" a life twice for the same wrong letter — a set fixes both the repeat and the double penalty.
  • Case matters: normalise both the word and the guess with .lower() or "A" never matches "a".
  • Recompute the masked display from word + guessed every turn; trying to edit the display string in place gets out of sync fast.

🚀 Performance & Best Practices

  • The whole game state is (word, guessed set, lives) — small enough to pass around explicitly, which makes it easy to test.
  • Win check is one line: set(word) <= guessed ("every letter has been guessed").
  • Load words from a file into a list once and random.choice per game rather than re-reading.

🧪 Try It Yourself

  1. Add a win/lose message and detect the win with set(word) <= guessed.
  2. Reject non-letters and already-guessed letters with a friendly message instead of a wasted life.
  3. Draw the classic ASCII gallows that grows as lives drop.

FAQ: Python Project: Hangman

Common questions about this page.

What is Python Project: Hangman?

Python Project: Hangman is a Python Projects lesson that explains python hangman project in Python. Guess letters in a hidden word. Track lives, reveal matches, and stop when the word is complete. Copy the samples and run them in the Python editor. It is written for beginners who want a clear definition and working examples.

Should I run python hangman project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn python hangman project in this Python Projects Python lesson (Python Project: Hangman).

How do I use python hangman project in Python?

To use python hangman project in Python, follow the examples on this StudyGrid page. Copy a snippet, run it in the browser, then Download and run it locally for better learning. Change the values and compare the output.

What is the syntax of python hangman project?

This Python Project: Hangman tutorial shows python hangman project syntax with short Python examples. Use the code blocks in this lesson for the exact statements, then try them in your editor.

Python Project: Hangman example for beginners

Yes. This page includes a beginner python hangman project example you can copy and run. It is designed for searches such as "python hangman project for beginners", "python hangman project example", and "how to use python hangman project".

What are common mistakes with python hangman project?

Common python hangman project mistakes include wrong syntax, mixing types, and skipping practice. Work through this Python Projects chapter in order, run every example, and check the output before moving on.

Why should I learn python hangman project?

Python Project: Hangman is used in real Python work. Learning python hangman project helps you write clearer programs and continue the Python Projects tutorial on StudyGrid.

Is Python Project: Hangman free to learn online?

Yes. You can learn python hangman project free on StudyGrid (studygrid.in). This chapter is part of the Python Projects path and includes examples, syntax, and next-step links.