Day 11: A Word Guessing Game In Python

Welcome to Day Eleven of my 21-day project series!

Today I have made a fun Word Guessing Game in python

It’s fun to play such things at times…

Do read the experience

Project Contents

This mini-project is a Word Guessing Game in Python.

It is a very simple implementation of this game. There are no fancy GUIs or stuff.

It doesn’t also have any functions. It’s all directly implemented in the main space.

I have used the random and the os library for this project.

Full Source Code

Here’s the full source code of A Word Guessing Game in Python.

import random
from os import system

# Word categories
categories = {
    "Coding": ["Python", "Variable", "Function", "Loop", "Algorithm", "Debugging", "Syntax", "Class", "Recursion", "Library"],
    "Gaming": ["Console", "Controller", "Quest", "Level", "Avatar", "Powerup", "Multiplayer", "Strategy", "Virtual", "Adventure"],
    "Movies": ["Action", "Comedy", "Thriller", "Drama", "Romance", "SciFi", "Fantasy", "Horror", "Animation", "Mystery"],
    "Travel": ["Beach", "Adventure", "Culture", "Explore", "Passport", "Destination", "Backpack", "Journey", "Sightseeing", "Vacation"]
}

# Get player name
player = input("Enter your name: ")

print()
print(f"\t\t\tWelcome to this word guessing game {player}.")
print("\t\t\tYou will get 10 chances to guess the word.")
print(f"\t\t\t\t\t\tAll The Best {player}")
print("\n\n")

# Print available categories
for i in categories.keys():
    print(f"{i}")

# Select category
category = input("Select category: ").title()
if category not in categories:
    print("Invalid category!")
    exit()

# Select a random word from the chosen category
word = random.choice(categories[category])

# Initialize guessed and wrong variables
guessed = len(word) * '_'
wrong = ""
chances = 10  # Total number of guesses

# Clear the console screen
system('cls')

# Display initial word with underscores
for i in word:
    print("_", end=" ")
print()
# Main game loop
while chances > 0:
    word = word.lower()
    guessed = guessed.lower()

    # Check if the player has guessed all the characters correctly
    if sorted(guessed) == sorted(word):
        print(f"Congrats {player}, you win!!")
        print(f"The correct word was, {word}")
        break

    # Ask the player to enter a character
    guess = input("Enter a character: ").lower()

    # Check if the guessed character is correct
    if guess in word:
        for i in range(len(word)):
            if word[i] == guess:
                tempg = list(guessed)
                tempg.pop(i)
                tempg.insert(i, guess)
                guessed = ''.join([elem for elem in tempg])
        
        # Display the current progress
        for i in range(len(word)):
            if word[i] == guessed[i]:
                print(word[i], end=" ")
            else:
                print("_", end=" ")
        
        print("\nYou are correct!!\n\n")
   
    # If the guessed character is incorrect
    elif guess not in word:
        wrong += guess
        print("\nYou are wrong!!\n\n")
        print(f"Chances left = {chances}")
        chances -= 1
        
# Player has used all the chances
if chances == 0:
    print(f"Alas {player}, You Lose!")
    print("Better luck next time...")
    print(f"The correct word was, {word}")
    print("Thanks for Playing, Have a nice day!")

This Word Guessing Game in Python is well documented however it has no docstring. Yet it is easy to understand.

Here are the links to topics within the mini-project. You can check them if you have any doubts or confusion:

Possible Improvements (That I’ll add after 21 days)

So, here are the possible improvements in the Word Guessing Game in Python:

  1. A GUI (I sincerely wanted to do this but I guess I was over-worried to miss it today)
  2. Two Player
  3. Difficulty Levels
  4. Time Limit
  5. Hint System
  6. Word Dictionary (for variety)
  7. Save/Load Game

And a lot more…

Experience of the day

In case you don’t know this project is a part of the brand new series that I have started. #21days21projects in python. To read more about it you can check out this page.

And for those who already know here’s my experience of the day.

First of all, It seems unbelievable to my eyes. Like 11 days straight onto something. I am still pinching myself.

Like never ever before in my life I have done anything this consistently. I lot has happened all this time. I had at least 20 reasons for not doing this.

Seriously, these are mostly the reasons I always quit stuff in around 4-5 days. I am amazed by myself this time.

If nothing else happens I am a better person definitely at the end of this 21 days challenge.

About the project, my dad told me that I should make something that everyone enjoys. There are not many readers like myself. I should think of stuff like games.

And that’s why I decided to make this Word Guessing Game.

Honestly, I wanted it to be a GUI. But, I was so worried about not getting this done on time that I just finished as fast as possible.

Do let me know whether you liked this Word Guessing Game. Also, let me know if there are any suggestions.

And most importantly how was your DAY 11?

Celebrating already?

Well, I hope it was fun. And remember it’s perfectly awesome if you missed out. Continue to work from tomorrow.

And today just write one line of code as simple as making a variable or printing something.

Your suggestions are welcomed!

Happy coding…

And wait, if you have some ideas for the upcoming projects do let me know that too. Even when the series ends do let me know what you like…

Leave a Reply