Hangman Game In Python With Source Code

A Hangman Game in Python where you guess letters (A-Z) to make words.

If the player correctly guesses the correct letter within the word, the letter appears in its proper place.

The user must guess the correct word until a man is hanged, at which point the game ends.

Project Details and Technology

Project Name:Hangman Game In Python with Source Code
Abstract:This Hangman Game is a fully functional game that attempts to provide leisure time to the users who avail of the game online.

This project is also for students who need this kind of project in Python.
Language/s Used:Python (GUI Based)
Python version (Recommended):3.8 or 3.9
Type:Desktop Application
Developer:Source Code Hero
Updates:0
Hangman Game – Project Information

About the Project

The Game is written in the Python computer language, and the purpose of this Hangman Game Project is to use Python to implement the Hangman Game. Other than random and time, it doesn’t require any further modules.

How To Make A Hangman Game In Python?

The code given below is the full source code on How To Make A Hangman Game.

The given code below is a Python file for Hangman.py

import random
import time

# Initial Steps to invite in the game:
print("\nWelcome to Hangman game by IT SOURCECODE\n")
name = input("Enter your name: ")
print("Hello " + name + "! Best of Luck!")
time.sleep(2)
print("The game is about to start!\n Let's play Hangman!")
time.sleep(3)


# The parameters we require to execute the game:
def main():
    global count
    global display
    global word
    global already_guessed
    global length
    global play_game
    words_to_guess = ["january","border","image","film","promise","kids","lungs","doll","rhyme","damage"
                   ,"plants"]
    word = random.choice(words_to_guess)
    length = len(word)
    count = 0
    display = '_' * length
    already_guessed = []
    play_game = ""

# A loop to re-execute the game when the first round ends:

def play_loop():
    global play_game
    play_game = input("Do You want to play again? y = yes, n = no \n")
    while play_game not in ["y", "n","Y","N"]:
        play_game = input("Do You want to play again? y = yes, n = no \n")
    if play_game == "y":
        main()
    elif play_game == "n":
        print("Thanks For Playing! We expect you back again!")
        exit()

# Initializing all the conditions required for the game:
def hangman():
    global count
    global display
    global word
    global already_guessed
    global play_game
    limit = 5
    guess = input("This is the Hangman Word: " + display + " Enter your guess: \n")
    guess = guess.strip()
    if len(guess.strip()) == 0 or len(guess.strip()) >= 2 or guess <= "9":
        print("Invalid Input, Try a letter\n")
        hangman()


    elif guess in word:
        already_guessed.extend([guess])
        index = word.find(guess)
        word = word[:index] + "_" + word[index + 1:]
        display = display[:index] + guess + display[index + 1:]
        print(display + "\n")

    elif guess in already_guessed:
        print("Try another letter.\n")

    else:
        count += 1

        if count == 1:
            time.sleep(1)
            print("   _____ \n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "__|__\n")
            print("Wrong guess. " + str(limit - count) + " guesses remaining\n")

        elif count == 2:
            time.sleep(1)
            print("   _____ \n"
                  "  |     | \n"
                  "  |     |\n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "__|__\n")
            print("Wrong guess. " + str(limit - count) + " guesses remaining\n")

        elif count == 3:
           time.sleep(1)
           print("   _____ \n"
                 "  |     | \n"
                 "  |     |\n"
                 "  |     | \n"
                 "  |      \n"
                 "  |      \n"
                 "  |      \n"
                 "__|__\n")
           print("Wrong guess. " + str(limit - count) + " guesses remaining\n")

        elif count == 4:
            time.sleep(1)
            print("   _____ \n"
                  "  |     | \n"
                  "  |     |\n"
                  "  |     | \n"
                  "  |     O \n"
                  "  |      \n"
                  "  |      \n"
                  "__|__\n")
            print("Wrong guess. " + str(limit - count) + " last guess remaining\n")

        elif count == 5:
            time.sleep(1)
            print("   _____ \n"
                  "  |     | \n"
                  "  |     |\n"
                  "  |     | \n"
                  "  |     O \n"
                  "  |    /|\ \n"
                  "  |    / \ \n"
                  "__|__\n")
            print("Wrong guess. You are hanged!!!\n")
            print("The word was:",already_guessed,word)
            play_loop()

    if word == '_' * length:
        print("Congrats! You have guessed the word correctly!")
        play_loop()

    elif count != limit:
        hangman()


main()


hangman()

To start executing a Hangman Game In Python, make sure that you have installed Python on your computer.

Steps On How To Run The Project

Time needed: 5 minutes

These are the steps on how to run the Hangman Game In Python With Source Code

  • Download Source Code

    First, find the downloadable source code below and click to start downloading the source code file.
    Rock Paper Scissors Game In

  • Extract File

    Next, after finished to download the file, go to the file location right-click the file and click extract.
    customer management system in php

  • Open PyCharm

    Next, open pycharm IDE and open the project you’ve downloaded.
    Blackjack Game In Python

  • Run Project

    Next, go to the Pycharm and click the run button to start executing the project.
    Space Invaders Game In Python

Download the Source Code below!

Summary

This Article is a way to enhance and develop our skills and logic ideas which is important in practicing the Python programming language which is the most well-known and most usable programming language in many companies.

Leave a Comment