Python Hangman

Python Hangman Code And Project 2023

Today in this article I  want to share Python Hangman Code And Project 2023, So our challenge is to make a Hangman game with hangman images including a random list of if-else and while loops. Hi Buddy my name is Amol and I am the founder of amolblog.com.

If you have any other requests or project ideas then please share them with YouTube Channel.

Please note: If you want only the final code please jump to the Final code see below table of contents.

Step 1 create words list

# Step 1 create words list
words = ["Africa", "Antarctica", "Asia", "Australia", "Europe", "North America", "South America", "Oceania", "Arctic", "Atlantic", "Indian", "Pacific", "Southern", "Artic", "Antarctic", "Mediterranean", "Caribbean", "Gulf of Mexico", "Gulf of Aden", "Red Sea" ]

# create word variable under random words choice
word = random.choice(words)

Step 2 set the alphabet Guess a letter

# Step 2 set alphabet Guess a letter
word_letters = set(word)
alphabet = set("abcdefghijklmnopqrstuvwxyz")
used_letters = set()

Step 3 create all hangman images

# Step 3 create all hangman images
hangman_images = [
    """
     +---+
     |   |
         |
         |
         |
         |
    =========
    """,
    """
     +---+
     |   |
     O   |
         |
         |
         |
    =========
    """,
    """
     +---+
     |   |
     O   |
     |   |
         |
         |
    =========
    """,
    """
     +---+
     |   |
     O   |
    /|   |
         |
         |
    =========
    """,
    """
     +---+
     |   |
     O   |
    /|\  |
         |
         |
    =========
    """,
    """
     +---+
     |   |
     O   |
    /|\  |
    /    |
         |
    =========
    """,
    """
     +---+
     |   |
     O   |
    /|\  |
    / \  |
         |
    =========
    """
]

Step 4 Use a while loop to input the latter

# Step 4 Use wile loop to input latter
mistakes = 0
while mistakes < len(hangman_images) - 1:
    # show the player their progress
    for letter in word:
        if letter in used_letters:
            print(letter, end=" ")
        else:
            print("_", end=" ")
    print()
    print(hangman_images[mistakes])

    # get the player's next guess
    guess = input("Guess a letter: ").lower()
    if guess in alphabet - used_letters:
        used_letters.add(guess)
    else:
        print("You've already guessed that letter.")

    # check for win
    if guess in word_letters:
        word_letters.remove(guess)
    else:
        # handle a wrong guess
        mistakes += 1
        print("Incorrect!")

Step 5 print lose or win using if else

# Step 5 print lose or win using if else
print("The word was", word + "!")
if mistakes == len(hangman_images) - 1:
    print("You lose! Better luck next time.")
else:
    print("You win!")

Final code

import random

# Step 1 create words list
words = ["Africa", "Antarctica", "Asia", "Australia", "Europe", "North America", "South America", "Oceania", "Arctic", "Atlantic", "Indian", "Pacific", "Southern", "Artic", "Antarctic", "Mediterranean", "Caribbean", "Gulf of Mexico", "Gulf of Aden", "Red Sea" ]

# create word variable under random words choice
word = random.choice(words)

# Step 2 set alphabet Guess a letter
word_letters = set(word)
alphabet = set("abcdefghijklmnopqrstuvwxyz")
used_letters = set()

# Step 3 create all hangman images
hangman_images = [
    """
     +---+
     |   |
         |
         |
         |
         |
    =========
    """,
    """
     +---+
     |   |
     O   |
         |
         |
         |
    =========
    """,
    """
     +---+
     |   |
     O   |
     |   |
         |
         |
    =========
    """,
    """
     +---+
     |   |
     O   |
    /|   |
         |
         |
    =========
    """,
    """
     +---+
     |   |
     O   |
    /|\  |
         |
         |
    =========
    """,
    """
     +---+
     |   |
     O   |
    /|\  |
    /    |
         |
    =========
    """,
    """
     +---+
     |   |
     O   |
    /|\  |
    / \  |
         |
    =========
    """
]

# Step 4 Use wile loop to input latter
mistakes = 0
while mistakes < len(hangman_images) - 1:
    # show the player their progress
    for letter in word:
        if letter in used_letters:
            print(letter, end=" ")
        else:
            print("_", end=" ")
    print()
    print(hangman_images[mistakes])

    # get the player's next guess
    guess = input("Guess a letter: ").lower()
    if guess in alphabet - used_letters:
        used_letters.add(guess)
    else:
        print("You've already guessed that letter.")

    # check for win
    if guess in word_letters:
        word_letters.remove(guess)
    else:
        # handle a wrong guess
        mistakes += 1
        print("Incorrect!")

# Step 5 print lose or win using if else
print("The word was", word + "!")
if mistakes == len(hangman_images) - 1:
    print("You lose! Better luck next time.")
else:
    print("You win!")

How to create Create hangman word list in python?

Step 1 create words list
words = [“Africa”, “Antarctica”, “Asia”, “Australia”, “Europe”, “North America”, “South America”, “Oceania”, “Arctic”, “Atlantic”, “Indian”, “Pacific”, “Southern”, “Artic”, “Antarctic”, “Mediterranean”, “Caribbean”, “Gulf of Mexico”, “Gulf of Aden”, “Red Sea” ]
create word variable under random words choice
word = random.choice(words)

How to convert variables in random choice in python?

create word variable under random words choice
word = random.choice(words)

Scroll to Top
Mother’s Day Python Code Love calculator Miss Universe And You Check Your Love Score With K-POP Idols Check Your Love Score With Childhood Crush