Python For GCSE
Adventures In Wordle
You can use the guidance on this page to get yourself a basic playable version of Wordle for Python. You can extend the program to use it to help you find the solution to Wordle games.
Step 1 - Word List
The first step is to get yourself a word list. Here is one that you can use to get you started. Download the file and save it to the folder where you are going to save your Python program.
We will read this into a list variable called words. You can use the technique described on this page for reading lines into a list.
Do this and then test that it has worked by printing out some or all of the results.
Step 2 - Setting Up
At the top of the program (before other lines of code), you will be setting up a few other things.
from random import choice yes = "y" no = "." maybe = "?" win = yes * 5
The first statement imports the choice function from the random module. We will use this to get Python to pick a random word from our list of words.
The other variables are the symbols we will be using in the feedback we get after each guess in the game. By setting them here, we can change them easily. The win variable is 5 of whatever we have used for our 'yes' symbol.
Step 3 – Evaluating A Guess
This function is the key to this program. If you can correctly evaluate a guess, you can make the game quite easily.
Here is the pseudocode,
Function check_pattern(guess, word) Result = "" used = [False, False, False, False, False] For i = 0 to 4 If guess[i] == word[i] Then Result = Result + yes Else For j = 0 To 4 If guess[i]==word[j] Then If guess[j] != word[j] Then If used[j]==False Then Result = Result + maybe used[j] = True Break End If End If End If Next j End If If Result.Length < i + 1 Then Result = Result + no End If Next i Return Result End Function
You need to test that this gives the correct feedback for a guess against a word. The code is similar to that used in Mastermind. Read the page for that for some guidance and explanation of that algorithm.
Step 4 – Play The Game
This is just about making as many guesses as you want to allow before either winning or stopping. Here is the pseudocode.
Procedure play_game() Attempts = 0 The_word = choice(words) Guessed = False While Attempts < 6 and guessed==False This_guess = "" While This_guess.length != 5 This_guess = Input("Enter your guess ").upper End While Attempts = Attempts + 1 Feedback = check_pattern(This_guess, The_word) If Feedback == win Then Print("You got it.", win) Guessed = True Else Print(Feedback) End If End While If Guessed == False Then Print("You ran out of guesses. The word was ", The_word) End If End Procedure
Test your game at this point and sort out any mistakes. Play a few rounds and maybe even win once in a while.
Step 5 – Solver
One approach to solving that is less than optimal is to make choose a random word from a list of possible words. After each guess, the feedback is used to narrow the list down to ever smaller numbers of words. This works far better than you might expect.
Procedure solve() Wrds = words.copy() F = "" While f != win and wrds.length>=1 Print(Wrds.Length, " words left") G = choice(wrds) Print("Try",G) F = input("Enter the feedback for guess " + g + ":") Newlist =  For each w in Wrds If check_pattern(G, W)==F Then Newlist.append(W) End If Next Wrds = Newlist.copy() End While End Procedure
The last procedure depends on you typing in the values carefully.
Step 6 – Refining
You could add some polish to the game by outputting lists of used letters or lists of letters that have not been counted out (ie not been a no in a previous attempt). This would help the player.
You could also perform some basic validation to prevent the programmer from making avoidable mistakes.
Making a better first guess would do no harm. You could look through the word list and choose a few decent starting words and randomly pick one of those to use.
Look carefully at the last procedure and you might be able to work out a tiny flaw in the algorithm that you could easily fix with an extra if statement. It's about what should and should not be added to the filtered list.
Step 7 – Autogame
Getting the program to play the game multiple times can let you find out the average number of steps required by the solver.
Write yourself a function that plays a game and returns the number of attempts needed to find the word or, -1 if it failed to do so in 6 attempts or fewer.
Now write a procedure to run n trials of the autogame, keeping track of the number of attempts taken each time. After all of the trials are conducted, output the number of wins, losses and the average number of attempts taken.
Step 8 – Some Menus
Some nice menus would set the game off a treat.
Step 9 – Variants
You could make the odd variation on the game. You could try some word lists of different lengths and see what impact that has on the game.
If you look for a downloadable scrabble word list, you can write yourself a Python program to pick out the words of the length you desire and write you a new file with them in.
Step 10 – More Study
One thing you might want to do is analyse the list of words you have more carefully. You could, for example, check the frequencies of letters and use that to inform your guessing.
Another thing you might do is get your early few guesses to deliberately target words that have letters that have not already been used. You could also make a list of ‘first guess words’ that have no repeating letters or which contain decent numbers of vowels.