Create Your Own Wordle Game in Python

Step-by-Step Guide for Beginners

Are you ready to build a fun and popular word game in Python? Today, we’ll create a simplified version of the famous game Wordle. This project is perfect for beginners, and it will help you understand important programming concepts like loops, conditionals, and lists. Let's get started!

 

What You Will Learn

  • Loops: How to repeat actions until a condition is met.

  • Conditionals: How to make decisions in your code.

  • Lists: How to store and manipulate collections of items.

  • String Manipulation: How to work with text data.

Step-by-Step Guide to Building Wordle

 

Step 1: Set Up the Game

First, we need to define the list of possible words and choose a random word from this list as the secret word. We’ll also need a variable to keep track of the number of attempts.

 

import random

def setup_game():

    words = ["apple", "grape", "peach", "berry", "melon"]

    secret_word = random.choice(words)

    attempts = 6

    return secret_word, attempts

 

# Initialize the game

secret_word, attempts = setup_game()

 

Step 2: Get User Guess

We’ll create a function to get the player’s guess and ensure it’s a valid word.

 

def get_user_guess():

    guess = input("Enter your guess (5-letter word): ").lower()

    while len(guess) != 5 or not guess.isalpha():

        print("Invalid guess. Please enter a 5-letter word.")

        guess = input("Enter your guess (5-letter word): ").lower()

    return guess

 

Step 3: Compare the Guess

We need to compare the user’s guess with the secret word and provide feedback. We’ll show ‘G’ for correct letters in the right place, ‘Y’ for correct letters in the wrong place, and ‘-’ for incorrect letters.

 

def compare_words(secret_word, guess):

    result = []

    for i in range(len(secret_word)):

        if guess[i] == secret_word[i]:

            result.append('G')  # Correct letter and position

        elif guess[i] in secret_word:

            result.append('Y')  # Correct letter, wrong position

        else:

            result.append('-')  # Incorrect letter

    return ''.join(result)

 

Step 4: Play the Game

Now, we’ll put everything together to run the game. We’ll loop through the attempts, get the user’s guess, compare it with the secret word, and provide feedback until the user guesses correctly or runs out of attempts.

 

def play_game():

    secret_word, attempts = setup_game()

    print("Welcome to Wordle!")

    print(f"You have {attempts} attempts to guess the word.")

 

    while attempts > 0:

        guess = get_user_guess()

        result = compare_words(secret_word, guess)

        print("Feedback: ", result)

 

        if guess == secret_word:

            print("Congratulations! You guessed the word!")

            break

 

        attempts -= 1

        print(f"Attempts left: {attempts}")

 

    if attempts == 0:

        print(f"Sorry, you ran out of attempts. The secret word was '{secret_word}'.")

 

# Start the game

play_game()

Wordle in Python

Sponsored
Connecting Dots@dharmesh on startups, scaleups and strategy

Full Wordle Python Code

Access and Make a Copy via Link below:

Conclusion

And there you have it! You’ve just built your own version of Wordle in Python. This game is a fantastic way to practice loops, conditionals, lists, and string manipulation—all essential skills for any budding programmer.

Recommendation

Sponsored
Connecting Dots@dharmesh on startups, scaleups and strategy

Want to Learn More?

If you enjoyed this tutorial and want to dive deeper into Python and other exciting projects, subscribe to our newsletter. You’ll get access to more fun projects, programming tips, and a free cheat sheet to enhance your Python skills!

🌟 Subscribe Now and Boost Your Coding Skills!

Happy coding and enjoy your game of Wordle!