liveBook · Manning

Correct digit

As explained in Learn AI-Assisted Python Programming, Second Edition

A correct digit is a guessed digit that matches the digit in the corresponding position of the secret code.

A correct digit is a guessed digit that matches the secret code at the same position. In a guess of 3821 against 1862, only 8 is correct, so the result includes one correct digit. section 10.3.1

Use the distinction between correct and misplaced digits to interpret feedback:

  • Count a digit as correct when guess[i] equals secret_code[i]. section 10.3.4
  • Check for a misplaced digit only when the positional comparison is not correct; this prevents one digit from receiving both labels. section 10.3.4
  • Return the correct count together with the misplaced count as feedback for each valid guess. section 10.3.2

Comparing a guess with the secret code

For secret code 1862 and guess 3821, 8 occupies the same second position, giving 1 correct digit. Digits 2 and 1 appear elsewhere, giving 2 misplaced digits. The function therefore returns [1, 2]. section 10.3.1 section 10.3.4

Table 10.1 Example of playing the game
Guess
Misplaced
Correct
Comments
0123
1
0
One of 0, 1, 2, 3 is in the answer; none are in the correct location.
4567
3
0
Three of 4, 5, 6, 7 are in the answer; none are in the correct location.
9045
0
1
Because one number from 0123 and three numbers from 4567 are in the answer, we know 8 and 9 aren’t in the answer. We know at least one of the numbers 4 or 5 must be in the answer from prior guesses and that 0 could be in the answer. One correct means that either 4 or 5 is in the correct location, either 4 or 5 isn’t present in the solution, and 0 isn’t in the solution.
9048
0
0
We know 8, 9, and 0 aren’t in the answer from prior guesses. Zero correct and zero misplaced tells us 4 is also not in the answer, and from the previous guess, we now know that 5 is the last digit.
1290
1
0
Going back to the original guess, we want to know which digit of 1, 2, and 3 is in the answer. We know 9 and 0 aren’t in the answer, so one misplaced means either 1 or 2 is in the answer and 3 isn’t in the answer. In addition, whichever of the numbers 1 and 2 are in the answer, it’s currently in the wrong spot.
6715
2
1
Because 4 isn’t in the solution, we know from the second guess that 5, 6, and 7 are. Our guess here tells us that 1 isn’t in the answer and that 6 and 7 are in the wrong place. Since 1 isn’t in the answer, 2 must be (from the previous guess). Because 5 is at the end and we’ve tried 2 in the second and third position previously with zero correct, 2 must be in the first position. Because we’ve tried 6 in the first and third position and neither were correct, 6 must be in the second position. That leaves 7 for the third position. We’ve got it.
2675
0
4
Yes, this is correct.

Follow the table to see how successive correct-digit counts eliminate possibilities and narrow the secret code. section 10.3.1

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Explains positional comparison, distinguishes misplaced digits, and tests the logic without covering duplicate-digit scoring.
Examples
Demonstrates 1862 versus 3821, 1234 versus 4321, and successive deduction from guesses in a feedback table.
What it assumes
Assumes strings, indices, loops, and conditional logic while treating secret-code and guess validation as separate subtasks.
What you can do after
Build it from scratch by comparing corresponding string positions and returning correct and misplaced counts from a Python function.
How it connects
Connects correct digits with misplaced digits, positional comparison, functions, testing, and Wordle-style feedback.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.