Skip to content
BetaDE
Lesson 7 of 35Python

Python · Lesson 7 · 40 minutes

Project: Hangman

What you'll learn

  • Process text letter by letter
  • Work with a set of guessed letters
  • Build a complete guessing game with a while loop

The game

In hangman, someone thinks of a word and the others guess letters. Correct letters are revealed, wrong ones cost a mistake. Whoever guesses the word before running out of mistakes wins.

You already know everything you need: strings, loops, lists and if. Only one small thing is new – a set.

A set for the guessed letters

A set is written with curly braces. It remembers every value only once – perfect for guessed letters:

guessed = set()          # an empty set
guessed.add("a")
guessed.add("e")
guessed.add("a")         # already in there – changes nothing
print(guessed)
print("e" in guessed, "x" in guessed)

Letter by letter

word = "kitten"
guessed = {"i", "t"}
for letter in word:
    if letter in guessed:
        print(letter, end=" ")
    else:
        print("_", end=" ")
print()

" ".join(list) turns a list into text with spaces in between: " ".join(["k", "_"]) gives "k _".

The whole game

This is what the finished game looks like – it uses exactly the two functions from your task. Try it (input appears below the editor):

def show(word, guessed):
    return " ".join(l if l in guessed else "_" for l in word)


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


word = "school"
guessed = set()
mistakes = 0

while mistakes < 6 and not won(word, guessed):
    print(show(word, guessed), f"   (mistakes: {mistakes}/6)")
    guess = input("Letter: ").lower()
    if guess in guessed:
        print("You already had that one!")
    elif guess in word:
        print("Correct!")
    else:
        print("Sorry, no.")
        mistakes += 1
    guessed.add(guess)

if won(word, guessed):
    print("You won! The word was", word)
else:
    print("You lost! The word was", word)

Keep building: let random.choice(["school", "python", "robot"]) pick a random word – and share your game with your friends via “Share” → 🎮 as a link.

Your task

Your turn

Write two functions for the game. show(word, guessed) returns the word the way the players see it: guessed letters visible, all others as _, each separated by one space – show("python", {"p", "o"}) gives "p _ _ _ o _". won(word, guessed) returns True when every letter of the word has been guessed.

def show(word, guessed):
    pass


def won(word, guessed):
    pass


print(show("python", {"p", "o"}))
print(won("mum", {"m", "u"}))

Solve it without hints to earn a ⭐ on your progress page.

Show solution

Try it yourself first – mistakes teach you the most. Here is one possible solution:

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


def won(word, guessed):
    for letter in word:
        if letter not in guessed:
            return False
    return True


print(show("python", {"p", "o"}))
print(won("mum", {"m", "u"}))