Repeating and Remembering

Repetition and Games


Learning Objectives

  • You can use an index-based loop to compare corresponding positions in two strings.
  • You can order equality and membership checks from the more specific case to the more general case.
  • You can build one result for every processed position.
  • You can separate a game’s rules from the framework that displays their results.

In Part 1, you changed the decision rules inside a graphical program without needing to understand the surrounding Pyxel framework. You will take the same approach here, but this time one game rule must be repeated for every character in a word.

Word Clues randomly chooses a secret from a list of candidate words and compares it with a word the player types. The player clears the current word with Backspace and checks a complete word with Space. Pressing 7 toggles a debug view of the secret so that the clues can be checked against a known word while testing. Every guessed character needs a clue saying whether it matches at that position, occurs somewhere else in the secret, or is absent. The exercise handout defines the exact strings that represent those clues.

Comparing Corresponding Positions

A direct loop provides one character but not its position. When a rule needs to compare corresponding positions in two sequences, an index-based loop is a better fit.

Consider two words of the same length:

reference = "CLOUD"
attempt = "CORAL"
for position in range(len(reference)):
expected_character = reference[position]
attempted_character = attempt[position]

The shared position connects the two sequences. During each iteration, the two variables hold the characters that should be compared with each other.

The order of related comparisons can matter. When two characters are equal, the attempted character is necessarily also a member of the reference string. If those cases should produce different results, the equality case must be considered before the broader membership case. elif is important here: after the first true case, Python must skip the remaining alternatives so that one position receives only one clue.

The decision should proceed from the most specific case to the remaining cases:

  1. first check whether the two current characters are equal;
  2. otherwise check whether the attempted character occurs somewhere in the reference;
  3. otherwise it is absent.

These rules check each position independently. They do not count how many times a letter occurs, as some word games do. Follow the simplified rules in the exercise handout, even when a guess repeats a letter.

Trace Before You Code

Trace the word comparison one position at a time:

positionAttemptedExpectedEqual?Attempted character in reference?First matching case
0"C""C"yesyessame position
1"O""L"noyespresent elsewhere
2"R""O"nonoabsent
3"A""U"nonoabsent
4"L""D"noyespresent elsewhere

This trace separates two questions that are easy to blur together: whether the current positions match, and whether a character occurs anywhere in the reference.

Build One Result Per Position

When you build the clue list, it starts empty. Its first position does not exist yet, so code cannot begin by replacing item 0. Each iteration must add its chosen result with .append(...).

Exactly one result should be appended on every iteration. If a branch appends nothing, the result list no longer stays aligned with the processed input positions. If two branches append during one iteration, the result list becomes too long. An if/elif/else chain can make the cases exclusive.

After each iteration, check that the result list contains one clue for every character processed so far.

Keep Game Logic Separate from Drawing

The loop you will write produces information about the guess: a list of clue names. It does not need to decide where tiles appear on the screen.

The framework uses each clue to choose a tile color. You can check your loop by comparing those colors with the secret word shown in debug mode.

Complete Word Clues

The exercise provides a runnable Pyxel starter and instructions for the missing loop. Run the starter, then change only its clearly marked section. You do not need to understand class, def, self, or the drawing and keyboard code.

Word clues

0 / 20 points

Run the starter program. It opens Word Clues, a small Pyxel game that chooses its secret at random from a list of twenty candidate words.

There is no preselected guess: enter every guess by typing its letters. Press Backspace to clear the whole word and Space to check it. The game checks a guess only after you have typed the same number of letters as the secret.

Press 7 to toggle a debug display of the secret. Use it to compare the clues with a known word while testing, then press 7 again to hide the secret.

You do not need to understand class, def, self, or the drawing and keyboard code. Change only the area between YOUR CODE STARTS HERE and YOUR CODE ENDS HERE inside WordClues.update.

The framework gives you three variables immediately before the marked area:

  • secret, the hidden string;
  • guess, an equally long string being checked;
  • clues, a new empty list for this submission.

Inside the marked area, write one index-based loop that begins with:

for index in range(len(secret)):

On every iteration, append exactly one status string to clues:

  1. append "exact" if guess[index] equals secret[index];
  2. otherwise, append "elsewhere" if guess[index] is in secret;
  3. otherwise, append "missing".

Use an if/elif/else chain in that order. An exact letter is also in secret, so checking membership first gives the wrong clue. Because clues starts empty, use .append(...); do not assign to clues[index].

This exercise uses simplified word rules. The framework checks only guesses with the same length as the secret. Apply the three rules independently at every position, even if a typed guess repeats a letter; you do not need to implement the more complicated repeated-letter accounting used by some word games. Your loop must work for any equal word length, not only the five-letter words shown by the game.

Leave the surrounding framework code unchanged. The automated tests simulate Pyxel without opening a window and separately check the random secret, keyboard controls and debug display, exact, elsewhere, and missing letters, mixed clues, different valid word lengths, and a second submission that must start with a fresh clue list.

Takeaways

You have changed a small part of a larger program using ideas you already know: indexes, decisions, and a result list. Trace the clues for a known word, run the game, and compare the display with your prediction.