Home CPSC 110

Wordle

 

December 8

 

Objective

To gain experience writing programs with functions and lists.


 

Task

For this assignment you will write a Python version of the popular game Wordle. In this game, the player has six tries to guess the randomly chosen 5-letter word.

Each of their guesses must be a real word. After each guess, the player is told if they got any letters exactly right, and whether they got any of the right letters but in the wrong positions.

For example, let's say that the secret word is "noise", and the user guesses "young". They will be told that the 'o' is in the right place. They will also be told there is an 'n', but not in the correct place.

Using this info, the user narrows down what the word can be and hopefully gets the right answer at the end.


 

Details

Because this is a complicated program, we will break it into six functions, which each solves one small part of this. Work on these functions one-by-one, testing each one when you are done with it. The functions you should write are:

  1. loadWords() - This first function's job is to load in the file words.txt which contains 4500 five-letter words. This is a little tricky, so the code for this function is given to you as a starting point:

    
    def loadWords():
        f = open("words.txt")
        words = []
        for word in f:
            words.append(word.rstrip())
        return words
    

    This function opens the file, and loops through each line in it. Each line gets added to a list of words which is then returned by the function. (The rstrip part removes a line ending character from the end of each word).

  2. pickWord(words) - this function takes in the list of all of the words. Its job is to randomly pick one and return it. You can use random.randint for this. Get a random number between 0 and the length of the list minus 1. Use this number as an index into the list of words and return the word there.

  3. isValid(guess, words) - the goal of this function is to tell us if the guess parameter is a valid guess or not. The function should return True if it is and False if not. To be valid, the guess should be five letters long and also be in the list of words. Remember you can use something like:

    
    if guess in words:
        # the guess is in the list of valid words!
    

    to determine this.

  4. checkGuess(guess, secret) - this function gives the user feedback on how close their guess was to the secret word. If the guess is actually equal to the secret word, it should return True.

    If it's not equal, it should give feedback. To do this, loop through the 5 letters the words have. For each spot, check if the letter matches. If so, print out that letter is exactly right. Then check if the letter is at least in the secret word (again using in). If so, tell them that word is there, but not in the right spot. Then return False.

  5. makeGuess(words, secret) - start this function by asking the user to enter a guess with input. Then write a loop to keep looping while there guess is not valid, by calling the isValid function you wrote. Once we know the guess is a valid one, call the checkGuess function. Whatever checkGuess returns should be returned from makeGuess too.

  6. game(words, secret) - this function should contain a loop that loops six times. At the start of each loop it should print a blank line with print(). That makes it so there's a blank between each guess to make it easier for the user to read the output.

    Then inside the loop it should call the makeGuess function. If it returns True, game should return True as well.

    After the loop, it should return False. This indicates that the user lost, since they didn't get it in the six tries they had.

Finally we need the part of the program that calls our functions and kicks things off. To do this, call the loadWords function and store the list of words in a variable called words.

Next, pick the secret word by setting a variable called secret equal to the pickWord function.

Next, call the game function, passing the list and secret word in as parameters. If it returns True, print a congratulatory message. Otherwise, print that they lost and show them what the secret word was.


 

Example Run

Here is an example run so that you can see how this program will work. This is a successful attempt by the user:

You have six tries to guess a five-letter word.

What is your guess? adieu
The i is right!
There is a u but not here.

What is your guess? unite
There is a u but not here.
The i is right!

What is your guess? quite
The q is right!
The u is right!
The i is right!

What is your guess? quilt
The q is right!
The u is right!
The i is right!

What is your guess? quips
Congratulations, you got it!

And here is an unsuccessful attempt:

You have six tries to guess a five-letter word.

What is your guess? angle
The e is right!

What is your guess? blame
The e is right!

What is your guess? trade
The t is right!
There is a r but not here.
The e is right!

What is your guess? there    
The t is right!
There is a e but not here.
There is a r but not here.
The e is right!

What is your guess? trite
The t is right!
There is a r but not here.
The t is right!
The e is right!

What is your guess? treat
The t is right!
There is a r but not here.
There is a e but not here.
There is a t but not here.
Sorry, the word is torte

 

General Requirements

When writing your program, also be sure to:


 

Submitting

When you are finished, please submit the .py file for the assignment on Canvas.

Copyright © 2024 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.