Hangman Game in Python Project for Beginners

3 min read

The best way to learn python is by doing projects. So hello python lovers today we develop Hangman Game in Python. Mini projects are best practice for beginners to get more interest in coding. So let’s code a hangman game in python.

What we gonna do?

Hangman game is just a word guessing game by guess the character of the word. In this game, there is a list of words present, out of which our interpreter will choose 1 random word.

The user first has to input their names and then, will be asked to guess any alphabet. If the random word contains that alphabet, it will be shown as the output(with correct placement) else the program will ask you to guess another alphabet. Users will be given 12 turns(can be changed accordingly) to guess the complete word.

Which python concepts are covered?

  • List
  • If-else
  • While loop
  • break statement

Full code of Hangman Game in Python

# library that we use in order to choose
# on random words from a list of words
import random

# Here the user is asked to enter the name first
name = input("Enter your name: ")
print("All the best", name)

words = [
    "website",
    "hangman",
    "rainbow",
    "computer",
    "science",
    "programming",
    "python",
    "mathematics",
    "player",
    "apple",
    "reverse",
    "water",
    "binod",
    "codesnail",
]

# Function will choose one random
# word from this list of words
word = random.choice(words)

print("\nGuess the characters")
guesses = ""

# any number of turns can be used here
turns = 12

while turns > 0:
    # counts the number of times a user fails
    failed = 0

    # all characters from the input
    # word taking one at a time.
    for char in word:
        if char in guesses:
            print(char, end="")
        else:
            print("_", end="")
            # for every failure 1 will be
            # incremented in failure
            failed += 1

    if failed == 0:
        # user will win the game if failure is 0
        # and 'You Win' will be given as output
        print("\n\nYou Win")

        # this print the correct word
        print("\nThe word is: ", word)
        break

    # if user has input the wrong alphabet then
    # it will ask user to enter another alphabet
    guess = input("\n\nguess the character: ")

    # every input character will be stored in guesses
    guesses += guess

    # check input with the character in word
    if guess not in word:
        turns -= 1

        # if the character doesn’t match the word
        # then “Wrong” will be given as output
        print("\nWrong")

        # this will print the number of
        # turns left for the user
        print("\nYou have", +turns, "more guesses")

        if turns == 0:
            print("\n\nYou lose")

Output

Enter your name: Iron-man
All the best Iron-man

Guess the characters
___________

guess the character: aa

Wrong

You have 11 more guesses
_____a_____

guess the character: w 

Wrong

You have 10 more guesses
_____a_____

guess the character: l

Wrong

You have 9 more guesses
_____a_____

guess the character: f

Wrong

You have 8 more guesses
_____a_____

guess the character: s

Wrong

You have 7 more guesses
_____a_____

guess the character: b

Wrong

You have 6 more guesses
_____a_____

guess the character: a 
_____a_____

guess the character: s

Wrong

You have 5 more guesses
_____a_____

guess the character: p
p____a_____

guess the character: prog
progra____g

guess the character: programming
programming

You Win

The word is:  programming

credit: Word Guessing Game

Hope you like this Hangman game in python. Now it’s your turn to extend this mini project. Share it with your friends.

More python projects 👇

Leave a Comment

Copy link
Powered by Social Snap