EX02 - One-Shot Wordle - Loops!


In this exercise you will use the concepts learned in the Lessons 9 and 10, as well as those of the previous unit. Please complete all lessons before attempting to begin this exercise.

The next step on our journey to implementing Wordle is to produce a program that gives the player one shot at guessing your program’s secret word. (Of course, they can play the game multiple times in order to have multiple chances.)

In this exercise, you will prompt the user for a word that matches the length of your secret word. We’ll use "python" for the purposes of our examples and autograding. By the end of this exercise, your program will be able to work with a secret word of any length, though!

You should follow the steps below for implementing the program one step at a time. To get a sense of where you are going, here is an example of a final game ():

    $ python -m exercises.ex02_one_shot_wordle
    What is your 6-letter guess? hello
    That was not 6 letters! Try again: whoopsy
    That was not 6 letters! Try again: jumped
    ⬜⬜⬜🟨⬜⬜
    Not quite. Play again soon!

    $ python -m exercises.ex02_one_shot_wordle
    What is your 6-letter guess? mythic
    ⬜🟩🟩🟩⬜⬜
    Not quite. Play again soon!

    $ python -m exercises.ex02_one_shot_wordle
    What is your 6-letter guess? photos
    🟩🟨🟨🟨🟩⬜
    Not quite. Play again soon!

    $ python -m exercises.ex02_one_shot_wordle
    What is your 6-letter guess? python
    🟩🟩🟩🟩🟩🟩
    Woo! You got it!

The emoji boxes of output have three possible states. Each box corresponds to a the corresponding index of the guessed word. A white box for a given index means that the guessed letter is not in the secret word, at all. A green box for a given index means that the guessed letter is correct at the same index of the secret word. A yellow box means the letter at that index is in the secret word, but not at the guessed position. Hence, for the secret word “python”, a guess of “photos” has green boxes at indices 0 and 4 because both words begin with ‘p’ and the second from last letter is ‘o’. The last box is white because the guessed letter was ‘s’ and “python” does not contain an ‘s’. Indices 1 through 3 are yellow because the letters at the guessed indices are in the word “python”, but they are not in the correct positions. If this does not yet make sense, try playing Wordle online for a chance to try it out!

Permitted Constructs

We expect you to implement this exercise using only the concepts covered in COMP110. If you have prior programming experience, restrict your implementation to only the concepts covered. While there are many ways to implement this program with additional concepts beyond those we have covered, you should not attempt to do so until after submitting this exercise for full credit once the autograder is posted. Gaining additional practice with the fundamentals may feel clunky, but will help ensure you have full command over the concepts we expect you to know. Additionally, it is good practice for working in other programming environments which are more constrained and require creativity to overcome restrictions. For this exercise, you will be penalized for using any kind of loop construct other than a while loop. Additionally, string methods (such as .count and .format) are not permitted.

Background Lesson: Advanced String Concepts

In this exercise you will need to make use of a few advanced string concepts:

  1. Emoji! 🤠
  2. Formatted Strings (f-Strings)

Before beginning work on this exercise, read the following lesson and complete the related questions on Gradescope: Unicode, Emoji, Escape Sequences, and f-sStrings

Part 0. Setting up the Python Program

In Visual Studio Code, be sure your workspace is open. (Reminder: File > Open Recent > comp110-YYS-workspace is a quick way to reopen it! Where YY is the current year and S is the semeseter: S for Spring, F for Fall.)

Open the Explorer pane (click the icon with two sheets of paper or to to View > Explorer) and expand the Workspace directory.

Right click on the exercises directory and select “New File”. Enter the following filename, being careful to match capitalization and punctuation:

  • ex02_one_shot_wordle.py

Before beginning work on the program, you should add a docstring to the top of your Python module just as you did in EX00 and EX01. Then, you should add a line with the special variable named __author__ assigned to be a string with your 9-digit student PID. (Disclaimer: Out in the real world the __author__ variable is typically your name and e-mail address, but since we will grade your programs we’d like to avoid potential bias in seeing your names as part of the programs as we’re grading.) Fill in your 9-digit UNC PID number, without any spaces or dashes, in the __author__ string.

Part 1. Establishing a Secret and Prompting for a Guess – 30 Points

In your program, establish a variable to hold your secret word and initialize it to the secret word of your choice. For autograding purposes, please use a secret word of "python". Once you have submitted for full autograding credit, you are encouraged to change your secret word to any word of your choice and see if your friends can solve it.

The first task of this program is to gather a guess from the user. Now that you have while loops at your disposal, you will not exit early when the incorrect number of letters is given. Instead, you will continue prompting the player until they provide a guess of the correct length. Remember, you can reassign a variable to be the result of prompting the player for input again. Here is how your program should perform at this step, if your secret word were “python”.

    $ python -m exercises.ex02_one_shot_wordle
    What is your 6-letter guess? hi
    That was not 6 letters! Try again: okk
    That was not 6 letters! Try again: whyy
    That was not 6 letters! Try again: nooooooo
    That was not 6 letters! Try again: tootle
    Not quite. Play again soon!

    $ python -m exercises.ex02_one_shot_wordle
    What is your 6-letter guess? snakes
    Not quite. Play again soon!

    $ python -m exercises.ex02_one_shot_wordle
    What is your 6-letter guess? python
    Woo! You got it!

Before you continue on: does this step of your program work correctly if you change your secret word to be a different number of letters? Your secret word should be assigned to a variable. Changing only that variable to a word with a different number of letters should not break anything else in your program. Specifically, your prompt strings should ask for the expected number of characters no matter what your secret word is. Remember, you can use len to find the length of a given string value.

Additionally, be sure you are using f-String templates rather than concatenation for this output, as introduced in the background lesson.

WARNING: Autograding will very specifically be looking for exactly the format of lines output shown above. You will not see the $ at your command-line prompt in VSCode, you can ignore that part. Otherwise, when you run the program on your machine with the same inputs as above on the first two lines, your printed results should look exactly like the 3rd line.

Part 2. Checking Indices for Correct Matches – 30 Points

Now that you have gathered a guess from Part 1, your task is to check each index of the word string to see if it matches (hint: is equal to) the corresponding index of your secret word. This check will be similar to chardle, except it will need to use a while loop in order to test each index of secret words of arbitrary lengths. Additionally, you will need to build up a string using concatenation with each index you check in the loop. You can use the following named constants in your program to simplify your implementation. We will learn more about named constants soon, but for now know they are simply variables whose values you will not change later in your program and make your programs easier to read:

WHITE_BOX: str = "\U00002B1C"
GREEN_BOX: str = "\U0001F7E9"
YELLOW_BOX: str = "\U0001F7E8"

Since loops are new to your repertoire, in this exercise we will give you a general strategy for accomplishing this task. Your job will be to translate this plan in English into working Python code.

  1. First, establish a variable to keep track of the index of the word you are checking and initialize it to the first index of a string.
  2. Additionally, establish a variable to store the resulting emoji of the guess and initialize it to an empty string.
  3. While the index variable is less than the length of the secret word, do as follows.
    1. Test to see if the current index of the guessed word is equal to the same index of your secret word.
      1. If so, concatenate a GREEN_BOX emoji to the result string variable you setup previously.
      2. Otherwise, concatenate a WHITE_BOX emoji.
    2. After the test, increase your index variable by one so that you do not have an infinite loop.
  4. Once the loop completes, print the resulting emoji string.

Notice, when this part of your program is completed, you will only have white and green boxes. In the final part of this program, you will add support for letting the player know a letter is in the word but at the wrong position. After this part, your program should behave as follows for a secret word of any length.

$ python -m exercises.ex02_one_shot_wordle
What is your 6-letter guess? photos
🟩⬜⬜⬜🟩⬜
Not quite. Play again soon!

$ python -m exercises.ex02_one_shot_wordle
What is your 6-letter guess? python
🟩🟩🟩🟩🟩🟩
Woo! You got it!

Once again, you should try changing your secret

Part 3. Checking Other Indices for Correct Letters at Incorrect Positions – 10 Points

Be sure you have completed Part 2 before beginning Part 3!

The real wordle will report yellow boxes for letters of a guess that are contained in the secret word but not in the correct position. In this part, you will add support for yellow boxes!

At this point, you have all the tools you need in order to figure this out for yourself. There are many ways of doing this, so we would encourage you to try on your own before reading the English description which follows and gives you a path towards completing this. “Pause the video here for a challenge.”

Did you get it? No, that’s ok, too. Here’s a strategy to try implementing this on your own.

Identify the location of your code that is evaluated when the current index of the guess being checked does not match the corresponding index of the secret. At this location, rather than simply deciding the guess was wrong, you will need to do a little more work to see if the guessed letter is found anywhere else in the secret. Once again, you will want to rely on a loop for this check so that you can change your secret word to be a word of any length later on (if you wrote a bunch of if-statements, like in Chardle, you would have to add or remove those statements each time your secret length changed).

Please implement the following algorithm for this part of the exercise:

  1. Find the location in your program you are certain the current index of the guess does not match the same index of the secret
  2. Declare a boolean variable to keep track of whether the guessed character exists anywhere else in the word and initialize it to False. Give it a simple, descriptive name.
  3. Declare a variable to keep track of the alternate indices of the secret you are checking for a match and initialize it to 0. Give it a simple, descriptive name.
  4. Write while loop with the following test logic: while the boolean variable is not true and the index is less than the length of the secret,
    1. Test to see if the alternate index of the secret word is equal to the current index of the guessed word.
      1. If so, assign True to your boolean variable.
      2. Otherwise, increment your alternate index variable.
  5. After the while loop completes, test to see if your boolean variable is True. (Check for understanding: what does it mean for this to be True?)
    1. If so, append the Emoji box that signals this outcome.
    2. Otherwise, append the other Emoji box.

Once you have completed this part of the program, your output should look as follows when using "python" as your secret word:

    $ python -m exercises.ex02_one_shot_wordle
    What is your 6-letter guess? photos
    🟩🟨🟨🟨🟩⬜
    Not quite. Play again soon!

    $ python -m exercises.ex02_one_shot_wordle
    What is your 6-letter guess? oneten
    🟨🟨⬜🟨⬜🟩
    Not quite. Play again soon!

    $ python -m exercises.ex02_one_shot_wordle
    What is your 6-letter guess? python
    🟩🟩🟩🟩🟩🟩
    Woo! You got it!

Once again, you should try changing your secret word variable to a word of a different length. Your program should still work correctly! For example, here’s how it should work if you chose the secret word "knoll".

    $ python -m exercises.ex02_one_shot_wordle
    What is your 5-letter guess? quest
    ⬜⬜⬜⬜⬜
    Not quite. Play again soon!

    $ python -m exercises.ex02_one_shot_wordle
    What is your 5-letter guess? help
    That was not 5 letters! Try again: hello
    ⬜⬜🟨🟩🟨
    Not quite. Play again soon!

    $ python -m exercises.ex02_one_shot_wordle
    What is your 5-letter guess? troll
    ⬜⬜🟩🟩🟩
    Not quite. Play again soon!

    $ python -m exercises.ex02_one_shot_wordle
    What is your 5-letter guess? knoll
    🟩🟩🟩🟩🟩
    Woo! You got it!

Before autograding submission, go ahead and change your secret back to "python".

Once you have this step of your program working, you should look back over the code you have written and convince yourself you understand the full logic of how it comes together! Congratulations on writing the essential logic of the same algorithm that the real Wordle uses.

Part 4. Style and Documentation Requirements – 20 Points (Manually Graded)

We will manually grade your code and are looking for good choices of meaningful variable names. Your variable names should be descriptive of their purposes. (Loop indexing variables can be short, such as i, or idx.) You should also use the Python snake_case convention where variable names are all lowercase and new words are separated by underscores.

You should add code comments in your own English words to describe what is happening at important stages of your program.

Your program should work regardless of the secret’s length. Thus, you should not have any hard-coded numbers (such as 6 for "python"). All numbers that appear in output and the boundaries of loops should be based on the len-gth of your secret word.

Part 6. Type Safety and Linting - 9 Points

The autograder uses industry standard tools for checking the type safety of your programs (e.g. being sure you’re using variables of the correct data types in valid ways) and linting style rules. If you have point deductions on Type Safety or Linting, read through the feedback the autograder gives and it should direct you to the line number in your program with the issue.

Make a Backup Checkpoint “Commit”

As you make progress on this exercise, making backups is encouraged.

  1. Open the Source Control panel (Command Palette: “Show SCM” or click the icon with three circles and lines on the activity panel).
  2. Notice the files listed under Changes. These are files you’ve made modifications to since your last backup.
  3. Move your mouse’s cursor over the word Changes and notice the + symbol that appears. Click that plus symbol to add all changes to the next backup. You will now see the files listed under “Staged Changes”.
    • If you do not want to backup all changed files, you can select them individually. For this course you’re encouraged to back everything up.
  4. In the Message box, give a brief description of what you’ve changed and are backing up. This will help you find a specific backup (called a “commit”) if needed. In this case a message such as, “Progress on Exercise 1” will suffice.
  5. Press the Check icon to make a Commit (a version) of your work.
  6. Finally, press the Ellipses icon (…), look for “Pull/Push” submenu, and select “Push to…”, and in the dropdown select your backup repository.

Submit to Gradescope for Grading

All that’s left now is to hand-in your work on Gradescope for grading!

Login to Gradescope and select the assignment named “EX02 - One-shot Wordle”. You’ll see an area to upload a zip file. To produce a zip file for autograding, return back to Visual Studio Code.

If you do not see a Terminal at the bottom of your screen, open the Command Palette and search for “View: Toggle Integrated Terminal”.

Type the following command (all on a single line):

python -m tools.submission exercises/ex02_one_shot_wordle.py

In the file explorer pane, look to find the zip file named “yy.mm.dd-hh.mm-exercises-ex01_chardle.py.zip”. The “yy”, “mm”, “dd”, and so on, are timestamps with the current year, month, day, hour, minute. If you right click on this file and select “Reveal in File Explorer” on Windows or “Reveal in Finder” on Mac, the zip file’s location on your computer will open. Upload this file to Gradescope to submit your work for this exercise.

Autograding will take a few moments to complete. For this exercise there will be 20 “human graded” points. Thus, you should expect a maximum autograding score of 80 possible points on this assignment. If there are issues reported, you are encouraged to try and resolve them and resubmit. If for any reason you aren’t receiving full credit and aren’t sure what to try next, come give us a visit in office hours!

Contributor(s): Kris Jordan