You can read the entire source code here

from random import choice

This pulls the function ‘choice’ from the random library. Choice is a function that, you guessed it, randomly chooses something, more specifically makes a choice from a list, the list of words.

dictionary = ["aargh", "aba...]

I got this huge dictionary by pulling a list of words from this repository and writing a script that sorts these into a Python list of 5-letter words. Because of that script, I don’t have to manually format everything. Anyways, I digress.

The choice function from above chooses one word form this list of dictionary words, and that will be the word the player will have to guess.

DEBUG_MODE = False  # Gives you the word while playing
NUMBER_OF_TRIES = 6  # 6 by default

These are options mostly for me to test things out. Debug mode gives you the answer after every guess, which was very useful to me making sure that the program recognized if I got the right answer. Number of tries is pretty self-explanatory.

Everything below repeats constantly until the user quits the program.

Below is the main game logic. Each round, the board is set up. This is the equivalent of the 6 guess results with yellow (lowercase) and green (uppercase) letters in normal Wordle.

for a in range(NUMBER_OF_TRIES):  # Number of tries
    board = ["-", "-", "-", "-", "-"]  # Result of user's guess
    while True: ...
    for i in range(5): ...
    if DEBUG_MODE: ...
    if ''.join(board) == answer.upper(): ...
    if a == NUMBER_OF_TRIES - 1: ...

Makes sure that guess is 5 letters or in the dictionary (list of words). This loop repeats until neither conditions are satisfied and break is called.

while True:  # Repeat until receiving a valid answer
    guess = input(f"{a + 1} > ").lower()

    if len(guess) != 5:
        print("That's not 5 letters!")
    elif guess not in dictionary:
        print("That's not in the dictionary!")
    else:
        break

This is how the program assembles the board. First, it finds letters in the correct spot and capitalizes them on the board. If a letter isn’t in the correct spot, the program checks each letter of the answer to see whether or not that letter is in the word. If the letter guessed in in the answer, it shows up on the board, but lowercase to indicate it’s not in the correct position.

Finally, it prints out the board.

for i in range(5):  # For each letter in the guess
    if guess[i] == answer[i]:
        # Capitalizes letters in correct spots
        board[i] = guess[i].capitalize()

    else:
        # Lowercases letters in answer but not in correct spots
        for j in range(5):
            if guess[i] == answer[j]:
                board[i] = guess[i].lower()

    print(f"? > {''.join(board)}")  # Prints characters of modified board

Built in cheating

if DEBUG_MODE:
    # Give the answer for 'testing purposes'
    print(f"d > {answer}")

Determines if you win or lose

if ''.join(board) == answer.upper():
    # If the board (all uppercase) is the same as the uppercase answer
    print("You win!")
    break

if a == NUMBER_OF_TRIES - 1:
    print("You lose!")
    print("The word was " + answer)
    break

Play again or don’t play again

input("Press enter to play again or Ctrl+C to quit.\n")