Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Use a Word List

00:00 Use a Word List. In this part of the course, you won’t change the functionality of your game, but you’ll make it more fun and replayable by adding a word list. So far, the secret word is always the same. That is about to change.

00:15 To do this, you’ll first create a small word list manually and integrate it into the game. Then you’ll explore how to turn any text into a word list. Your word list will be a plain text file containing one word per line.

00:30 This follows a long tradition on Unix systems where a file named words is used by spell checkers and similar applications. To get started, create a new file you call wordlist.txt with the content seen on-screen.

00:46 Here, you are adding words that could be the secret word. Feel free to expand the list yourself, but don’t put too much effort into it, as soon you’ll be creating the word list automatically.

00:57 Before creating the better word list, you’ll see how you can read the list of words into your program. Python’s pathlib module is great for working with files and reading them into memory. You can try it out in the REPL.

01:17 The .read_text() method reads the whole thing as one text string. Note that the \n symbols separate the words. These represent newlines in the file.

01:28 You can strip off the last newline and split on the rest to convert the file into a list of words.

01:49 You use .strip() to remove any extra lines at the end of the file and .split() to convert the file into a list of words. To avoid having to care about lower and uppercase, you convert all the words to uppercase.

02:04 You’re now able to include a list of words in your program, but how can you choose one random word from that word list? Python comes with a powerful random module in the standard library.

02:15 You can use it to generate all kinds of randomness in your projects. Here you’ll use random.choice(), which randomly chooses one item from a sequence.

02:36 As you can see, sometimes randomness gives you the same answer twice in a row. Your results will likely be different from the one seen on-screen, but it’s time to add word list functionality to the Wordle clone.

02:49 Edit the game as seen on-screen. First, the constant WORD is removed, as you’ll be replacing it with the code you’re about to enter. You import pathlib and random, as they’ll be used as you saw previously in the REPL.

03:05 The words are read from the word list and processed appropriately.

03:20 A word is then randomly chosen from the list.

03:26 The final edits to make are to replace the previous constant WORD with the new variable word, now in lowercase to show that it’s no longer a constant.

03:50 One small frustration is that if you don’t guess the word correctly, you’ll never get to know which secret word was chosen. To fix that, add these lines at the end of your code.

04:10 It’s not common to use the else clause with for, but it’s quite powerful in the right situation. The code inside the else will only run if the for loop terminates naturally, but it won’t run if that isn’t the case—here, if break stops the loop. In practice, this means that the secret word is printed if all the guesses are different from word. While you’re developing your game, you’ll run it many times. To test your code effectively, you may want to cheat and know the secret word up front. While testing your code, add the line seen on-screen just before calling input(), and this will print the secret word to the console.

04:52 Try running the game a few times. It’s already more challenging and fun since you don’t know the exact word up front, but with a limited word list, the game gets repetitive.

05:03 So in the next part of the course, you’ll look at a method to create larger word lists.

Become a Member to join the conversation.