Hangman Game In Python With Source Code

spyrokp@gmail.com Avatar
\"\"

Hangman is a classic word guessing game where one player thinks of a word

and the other player tries to guess the word by suggesting letters one at a time. If the guessing player suggests

a letter that is not in the word, the other player draws one part of a \”hangman\” figure. If the guessing player suggests a correct letter, the other player fills in all instances of that letter in the word. The game continues until either the guessing

player correctly guesses the word or the \”hangman\” figure is completed.

To build a Hangman game using Python, we\’ll need to define a few things. First, we\’ll need a list of words to choose from. This can be as simple or as complicated as you want – you could hardcode a list

of words into the program, or you could read from a file containing a large list of words.

1.

Next, we\’ll need to define the game logic. This will involve keeping track of the word being guessed, the letters that have been guessed, and the \”hangman\” figure. We\’ll also need to handle user input – taking in a letter from the guessing player and checking if it is in the word.

We can break down the game logic into smaller functions that handle specific tasks. For example, we could define a function to randomly choose a word from our list of words, a function to display the current state of the game (including the word being guessed and the \”hangman\” figure), and a function to handle user input.

We\’ll also need to think about how to display the game to the user. We could simply print the current state of the game to the console after each guess, or we could use a GUI library like tkinter to create a graphical user interface.

Overall, building a Hangman game using Python is a great exercise in programming fundamentals like data structures, functions, and flow control. Plus, it\’s a fun game to play and can be customized in many ways to make it your own!

First, we\’ll need to define a list of words to choose from. For example, we could use:

words = [\”apple\”, \”banana\”, \”cherry\”, \”dog\”, \”elephant\”, \”fruits\”, \”grape\”, \”hockey\”, \”igloo\”, \”jacket\”, \”kangaroo\”, \”lion\”, \”mango\”, \”nectarine\”, \”ocean\”, \”penguin\”, \”quilt\”, \”rainbow\”, \”strawberry\”, \”tiger\”, \”umbrella\”, \”violin\”, \”watermelon\”, \”xylophone\”, \”yacht\”, \”zebra\”]

Next, we\’ll define a function to choose a random word from the list:

import random

def choose_word(words):

    return random.choice(words)

We\’ll also need to define a function to display the current state of the game. We\’ll represent the \”hangman\” figure as a list of strings, with each string representing a row of the figure. We\’ll use underscores to represent unknown letters in the word.

HANGMAN = [

    \”__________\”,

    \”|         |\”,

    \”|         |\”,

    \”|         O\”,

    \”|        /|\\\\\”,

    \”|        / \\\\\”,

    \”|\”,

]

def display_game(word, guessed_letters):

    print(\” \”.join([letter if letter in guessed_letters else \”_\” for letter in word]))

    print(\”\\n\”.join(HANGMAN[:len(guessed_letters)]))

Next, we\’ll define a function to handle user input. This function will take in a list of guessed letters, prompt the user to enter a letter, and add the letter to the list if it hasn\’t already been guessed.

def get_guess(guessed_letters):

    while True:

        guess = input(\”Guess a letter: \”).lower()

        if len(guess) != 1:

            print(\”Please enter a single letter.\”)

        elif guess in guessed_letters:

            print(\”You already guessed that letter.\”)

        elif not guess.isalpha():

            print(\”Please enter a letter.\”)

        else:

            return guess

Finally, we\’ll put it all together in a loop that continues until the player either guesses the word or runs out of guesses:

MAX_GUESSES = len(HANGMAN)

word = choose_word(words)

guessed_letters = set()

while True:

    display_game(word, guessed_letters)

    guess = get_guess(guessed_letters)

    guessed_letters.add(guess)

    if guess in word:

        print(\”Correct!\”)

    else:

        print(\”Incorrect.\”)

    if set(word) <= guessed_letters:

        print(\”Congratulations, you guessed the word!\”)

        break

    elif len(guessed_letters) >= MAX_GUESSES:

        print(\”Sorry, you ran out of guesses.\”)

        break

And there you have it – a simple Hangman game in Python! Of course, you can customize and add more features to make the game your own.

Also Read: Best Python projects for beginners