Building a Python Hangman Game with Replit

learn to code games in python

Python Hangman Game

Learning to code is a fantastic journey, especially when you create something fun like a Hangman game! Today, we’ll guide you through building a classic Hangman game in Python using Replit. This project is perfect for beginners and helps reinforce fundamental programming concepts.

Getting Started

First, head over to Replit, a great online coding platform that supports various programming languages, including Python. If you don’t have an account yet, sign up—it’s free!

Once logged in, create a new Python repl:

  1. Click on the + button in the top right corner.
  2. Choose “Create Repl”.
  3. Select Python as your language.

Step-by-Step Guide

Step 1: Setting Up the Game

Start by setting up the basic structure of the Hangman game. Create a list of words that the player can guess from.

import random

words = ['python', 'programming', 'hangman', 'challenge', 'replit']
word = random.choice(words)
guesses = ''
turns = 10

Step 2: Main Game Loop

Create the main loop that will keep the game running until the player either wins or loses.

while turns > 0:
    failed = 0
    for char in word:
        if char in guesses:
            print(char, end=' ')
        else:
            print('_', end=' ')
            failed += 1

    if failed == 0:
        print("\nYou win!")
        break

    guess = input("\nGuess a character: ").lower()
    guesses += guess

    if guess not in word:
        turns -= 1
        print(f"Wrong guess. You have {turns} more guesses.")
        if turns == 0:
            print(f"You lose. The word was '{word}'.")

Step 3: Adding More Features

Enhance the game by adding more features, such as displaying the letters guessed so far and improving the user interface.

import random

def display_hangman(turns):
    stages = [
        """
           ------
           |    |
           |    O
           |   /|\\
           |   / \\
           -
        """,
        # Add other stages here...
    ]
    return stages[10 - turns]

words = ['python', 'programming', 'hangman', 'challenge', 'replit']
word = random.choice(words)
guesses = ''
turns = 10

print("Welcome to Hangman!")

while turns > 0:
    failed = 0
    for char in word:
        if char in guesses:
            print(char, end=' ')
        else:
            print('_', end=' ')
            failed += 1

    if failed == 0:
        print("\nYou win!")
        break

    print(display_hangman(turns))
    guess = input("\nGuess a character: ").lower()
    guesses += guess

    if guess not in word:
        turns -= 1
        print(f"Wrong guess. You have {turns} more guesses.")
        if turns == 0:
            print(f"You lose. The word was '{word}'.")

Conclusion

Building a Hangman game is a great way to practice Python programming. It incorporates essential concepts like loops, conditionals, and user input. Once you’re comfortable with this, try expanding the game with new features or creating your own projects!

Check out our other coding projects and tutorials to continue your learning journey. Happy coding!


SHARE WITH FRIENDS >

After-school Live Virtual Scratch & Block Coding Classes For Kids, Phoenix

20 Apr 2026

After-school Scratch classes in Phoenix: schedules, age groups, and what your child will learn

After-school Live Virtual Scratch & Block Coding Classes For Kids, Phoenix

20 Apr 2026

Phoenix coding classes for kids: Compare live virtual after-school Scratch & block coding options

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

Accelerated Math & Enrichment Programs for Gifted Expat Students in Dubai (Pre‑IB / AP / Early College Math)

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

FAQ: Everything Dubai parents ask about virtual North American math tutors (credentials, safety, results, booking)

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

Pricing, trials and guarantees: What Dubai parents should expect for premium North American math tutors

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

Parent guide to scheduling virtual math tutoring in Dubai: best times, timezone tips and tech checklist

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

Success stories: Dubai expat students who improved grades and SAT/AP scores with virtual North American tutors

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

Choosing Between One‑on‑One vs Small‑Group Virtual Math Tutoring for Expat Kids in Dubai

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

How Our Lessons Align with US Common Core and Ontario/BC Curricula — Sample Math Scope & Sequence by Grade