Word Guessing Game in Python

1. Introduction

In this blog post, we will write a Python program to implement the word-guessing game.

A word-guessing game challenges players to guess a word with a limited number of attempts. It's a simple yet engaging game that can be easily implemented in Python. This tutorial will guide you through creating a basic word-guessing game. The player will randomly select a word, and the player must guess it one letter at a time.

Word Guessing Game in Python

2. Program Steps

1. Create a list of words for the game.

2. Randomly select a word from the list for the player to guess.

3. Display the word to the player with letters hidden, showing only the letters they've correctly guessed.

4. Allow the player to guess one letter at a time, revealing correctly guessed letters.

5. Keep track of the number of incorrect guesses and end the game if the player exceeds this limit.

3. Code Program

import random

# Step 1: List of words to guess
word_list = ["python", "programming", "coder", "algorithm", "syntax"]

# Step 2: Randomly select a word
secret_word = random.choice(word_list)
guessed_letters = ["_"] * len(secret_word)
attempts_remaining = 6

# Function to display the current state of the word
def display_word():
    print("Word to guess: " + " ".join(guessed_letters))

# Main game loop
while "_" in guessed_letters and attempts_remaining > 0:
    display_word()
    guess = input("Guess a letter: ").lower()

    # Step 3 & 4: Check the guessed letter
    if guess in secret_word:
        for i in range(len(secret_word)):
            if secret_word[i] == guess:
                guessed_letters[i] = guess
    else:
        attempts_remaining -= 1
        print(f"Incorrect! You have {attempts_remaining} attempts remaining.")

# Step 5: Check the game outcome
if "_" not in guessed_letters:
    display_word()
    print("Congratulations! You've guessed the word correctly.")
else:
    print(f"Sorry, you've run out of attempts. The word was '{secret_word}'.")

Output:

Word to guess: _ _ _ _ _ _
Guess a letter: p
Incorrect! You have 5 attempts remaining.
Word to guess: _ _ _ _ _ _
Guess a letter: s
Word to guess: s _ _ _ _ _
Guess a letter: y
Word to guess: s y _ _ _ _
Guess a letter: n
Word to guess: s y n _ _ _
Guess a letter: t
Word to guess: s y n t _ _
Guess a letter: a
Word to guess: s y n t a _
Guess a letter: x
Word to guess: s y n t a x
Congratulations! You've guessed the word correctly.

Explanation:

1. The game begins by selecting a random word from word_list. This word is the secret the player tries to guess.

2. The player is shown the word as a series of underscores, one for each letter in the word, hiding its letters.

3. The player guesses one letter at a time. If the letter is in the word, the corresponding underscores are replaced with that letter.

4. Each incorrect guess decreases the number of attempts remaining. The game keeps the player informed of how many attempts they have left.

5. The game ends when the player either guesses all the letters of the word (winning the game) or runs out of attempts (losing the game). The final outcome is displayed to the player along with the correct word if they did not guess it.

Comments