The Word Guessing Game is a fun and engaging way to practice programming concepts in Python. In this game, the player has to guess a word chosen randomly from a predefined list of words. The game provides feedback on whether the guessed letters are correct and reveals the current state of the word being guessed.

How the Game Works

Implementation

Here’s a simple implementation of the Word Guessing Game in Python:

Code Example

Python
import random

def word_guessing_game():
    # List of words to choose from
    words = ["python", "java", "kotlin", "javascript", "ruby"]
    secret_word = random.choice(words)
    guessed_letters = []
    attempts = 6  # Number of allowed incorrect guesses
    word_completion = "_" * len(secret_word)

    print("Welcome to the Word Guessing Game!")
    print(f"The secret word has {len(secret_word)} letters: {word_completion}")

    while attempts > 0:
        guess = input("Guess a letter: ").lower()

        if len(guess) != 1 or not guess.isalpha():
            print("Please enter a single letter.")
            continue

        if guess in guessed_letters:
            print("You've already guessed that letter. Try again.")
            continue

        guessed_letters.append(guess)

        if guess in secret_word:
            print(f"Good job! '{guess}' is in the word.")
            # Update word completion
            word_completion = "".join(
                [letter if letter in guessed_letters else "_" for letter in secret_word]
            )
        else:
            attempts -= 1
            print(f"Sorry, '{guess}' is not in the word. You have {attempts} attempts left.")

        print(f"Current word: {word_completion}")

        if "_" not in word_completion:
            print(f"Congratulations! You've guessed the word: '{secret_word}'")
            break
    else:
        print(f"Sorry, you've run out of attempts. The secret word was '{secret_word}'.")

# Run the game
if __name__ == "__main__":
    word_guessing_game()

How to Run the Game

Example Output

Here’s an example of what playing the game might look like:

Word Guessing Game in Python

Conclusion

The Word Guessing Game is an excellent way to practice your Python programming skills, including working with loops, conditionals, lists, and string manipulation. You can enhance this game by adding features such as difficulty levels, hints, or even a graphical user interface (GUI) using libraries like Tkinter or Pygame. Enjoy coding and have fun playing your Word Guessing Game.