Kids Coding

Project: Hangman

Guess letters. Keep the secret word in a list. Stop when it is complete or lives are gone.

KidsIntermediateProject: Hangman

Guess letters, not the whole word

Keep the secret as a string. Keep a list of _ the same length. When a guess matches a letter, replace that _.

Replay three guesses

secret = "fox"
shown = ["_", "_", "_"]
guesses = ["o", "z", "f"]
lives = 3

for guess in guesses:
    print("Guess:", guess)
    if guess in secret:
        for i in range(len(secret)):
            if secret[i] == guess:
                shown[i] = guess
        print("Yes:", "".join(shown))
    else:
        lives = lives - 1
        print("No. Lives:", lives)

if "_" not in shown:
    print("You won")
else:
    print("The word was", secret)

Remember which letters were already guessed. Without that, a player can pick the same wrong letter twice and lose two lives for one mistake, which feels unfair.

What you practiced

  • Lists and indexes.
  • in to test membership.
  • A loop over guesses.

Real life

A word on the blackboard with missing letters.

The class guesses one letter. Right letters fill in. Lives are the chalk marks.

Board word

secret = "cat"
shown = ["_", "_", "_"]
guess = "a"
for i in range(len(secret)):
    if secret[i] == guess:
        shown[i] = guess
print("Board:", " ".join(shown))

Change one word. Run it. That is how this idea shows up outside the lesson.

Try it

Exercise 1: Start shown as ["_", "_"] for hi. If the guess is h, set index 0 to h and print shown.

Show solution
shown = ["_", "_"]
guess = "h"
if guess == "h":
    shown[0] = "h"
print(shown)

FAQ: Project: Hangman

Common questions about this page.

What is StudyGrid Kids?

StudyGrid Kids is a free coding path for children: Basic, Intermediate, and Advanced. Lessons use Python in the browser. No account and no install.

Should I run project: hangman 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 project: hangman in this Kids Coding Python lesson (Project: Hangman).

Which kids level should we start with?

Start at Basic if the child has never written code. Choose Intermediate if they can print, use variables, and write a simple if. Choose Advanced if they already use loops and lists.

Do kids need an account or an app?

No. Kids pages are public. Code runs in the Try Python editor at /try. We do not ask children for a name or an email.

What language do kids learn?

Python, with a first HTML page in Intermediate and Advanced. After Advanced, the main Python tutorial is the next step.

Is the kids coding track free?

Yes. The Kids home, the three levels, and the Python editor on StudyGrid (studygrid.in) are free.