Creating a Wordle Game Using Python

Creating a Wordle Game Using Python

ECX 30 Days of Code and Design

Day 7

Wordle

Wordle is a single-player game, in which a user is required to guess a 5-letter hidden word in 6 attempts.

Task
  • The user makes a first guess (e.g., "Skate").
  • Print out a progress guide like this; "√××√+".
  • "√" Indicates that the letter at that position was guessed correctly.
  • "+" indicates that the letter at that position is in the hidden word, but in a different position.
  • "×" indicates that the letter at that position is wrong, and isn't in the hidden word.
  • This process is repeated until the user either guesses the hidden word correctly—in which case, he wins—or exhausts his 6 attempts; thus, losing.
  • The "hidden word" is generated randomly from a list of 5-letter words hard-coded by you.

My Approach

This is the code. I would explain it in bits throughout this article.

import random               # For randint

word_dict = ['bread', 'broke', 'brace', 'boost', 'mouse', 'loose', 'crust', 'books', 'loops', 'sings', 'bloat',
             'broad', 'chair', 'table', 'seats', 'apple', 'brown', 'brook', 'phone', 'brush', 'jeans', 'clean',
             'paste', 'cloth', 'child', 'beans', 'brick', 'catch', 'goose', 'geese', 'cycle', 'india', 'china',
             'niger', 'lagos', 'delta', 'shirt', 'zebra', 'glass', 'river', 'print', 'burnt', 'madam', 'board',
             'going', 'fries', 'paper', 'drive', 'chill', 'brick', 'japan', 'funny', 'hatch', 'route', 'holes',
             'rides', 'worry', 'short', 'water', 'local', 'flown', 'leave', 'lives', 'kenya', 'three', 'truth',
             'awake', 'aside', 'happy', 'beads', 'model', 'trips', 'based', 'gowns', 'shops', 'seven', 'loses',
             'bride', 'groom', 'sweet', 'money', 'actor', 'place', 'stare', 'close', 'light', 'stand', 'woman',
             'creek', 'gates', 'brood', 'night', 'month', 'weeks', 'hands', 'drink', 'proud', 'eight', 'stall',
             'found', 'focus', 'tools', 'power', 'track', 'creed', 'house', 'maize', 'plate', 'cable', 'claps',
             'greet', 'uncle', 'flour', 'dance', 'maize', 'fifty', 'depth', 'zones', 'reply', 'black', 'shoes']

word_dict_span = len(word_dict)

word = word_dict[random.randint(0, word_dict_span - 1)]

print(' Wordle Game '.center(40, '*'))
print('/ indicates the correct letter and position.')
print('+ indicates the letter is in the word, but in the wrong position.')
print('x indicates that the letter is not in the word.\n')

print('You are to input a 5 letter word. You have 6 tries.')

for i in range(6):
    count = 0
    player_input = input('Enter the word: ').lower()

    if len(player_input) == 5:
        for j in player_input:
            if j in word:
                if word[count] == j:
                    print('/', end='')
                else:
                    print('+', end='')
            else:
                print('x', end='')

            count = count + 1

        if player_input == word:
            print('\nYou won')
            break

        if i == 5 and player_input != word:
            print('\nSorry, you lost. The word is', word)

        print('\n')

    else:
        print('Invalid entry! Enter a five letter word.')
        continue

First, we import the random module, and we create a list of words which we would use for the gameplay.

import random               # For randint

word_dict = ['bread', 'broke', 'brace', 'boost', 'mouse', 'loose', 'crust', 'books', 'loops', 'sings', 'bloat', … , 'shoes']

We then find the number of list items in the list, and we create a variable, word, which generates random words from the word_dict, which the player would have to decipher. Next, we print out the instructions the player is to be aware of.

word_dict_span = len(word_dict)

word = word_dict[random.randint(0, word_dict_span - 1)]

print(' Wordle Game '.center(40, '*'))
print('/ indicates correct letter and position.')
print('+ indicates letter is in the word, but wrong position.')
print('x indicates that the letter is not in the word.\n')

print('You are to input a 5 letter word. You have 6 tries.')

We then use a for loop which would allow the player six tries before the game ends (if the player fails to get the correct word). Next, we create a variable, count, which we would use to check if the individual letters in the word inputted by the player are in the correct positions in the word to be guessed. The player is then prompted to input the word, which is made a lower case to conform with the values in the word_dict.

for i in range(6):
    count = 0
    player_input = input('Enter the word: ').lower()

When the player inputs a word, we check if it is a 5 letter word; if it is not, we prompt the player to enter a valid move and continue the loop.

    if len(player_input) == 5:
     --snip--
    else:
        print('Invalid entry! Enter a five letter word.')
        continue

If the player enters a 5 letter word, each letter is checked if it exists in the word to be guessed; if a letter is in the word, it is then checked if it is in the right position. If it is in the right position, we get ‘/’, else we get ‘+’ to indicate that the letter exists in the word but it is not in the correct position. If the letter does not exist in the word, we get ‘x’. When the word has been correctly guessed, we get ‘/////’ and ‘You won’ is printed on the screen. However, if the player fails to get the word after 6 tries, we tell s/he has lost and the word that was to be guessed.

    if len(player_input) == 5:
        for j in player_input:
            if j in word:
                if word[count] == j:
                    print('/', end='')
                else:
                    print('+', end='')
            else:
                print('x', end='')

            count = count + 1

        if player_input == word:
            print('\nYou won')
            break

        if i == 5 and player_input != word:
            print('\nSorry, you lost. The word is', word)

        print('\n')

    else:
        print('Invalid entry! Enter a five letter word.')
        continue

Example

************* Wordle Game **************
/ indicates the correct letter and position.
+ indicates the letter is in the word, but in the wrong position.
x indicates that the letter is not in the word.

You are to input a 5 letter word. You have 6 tries.
Enter the word: bread
//xxx

Enter the word: brink
///x/

Enter the word: brick
/////
You won

Play the game on Replit