the codebreaker challenge
Python game where players guess a random four-digit number, counting correct digit placements without revealing positions.
Imagine you're a top-secret agent on a mission to crack an enemy's secure combination lock! The lock has a secret four-digit code, and you need to guess it. With each attempt, the system will tell you how many digits you got exactly right and in the correct place, but it won't give away which ones they were. Can you crack the code in as few tries as possible?
Required Prior Learning
Using
input() and print() to interact with the user.Using
if statements to make decisions in your code.Using
for loops and while loops to repeat actions.Working with basic variables and data types like numbers and text (strings).
Use
Investigate & PredictTake a look at the Python code in the sandbox below. It shows how to compare two fixed four-digit codes and count how many digits are exactly the same and in the same position. Run the code and observe what happens before answering the prediction questions.
Prediction Questions
1
What value will be printed for
correct_digits_in_position in the first example, where the secret is "1234" and the guess is "1537"?2
Explain how the
for i in range(len(secret_code)): loop helps the program compare each digit individually.3
If you were to change the
user_guess_2 variable to "9877", what value would then be printed for correct_digits_in_position_2?Modify
Tweak & ExperimentTake the starter code in the widget above and experiment with the following changes:
1
Change the
secret_code in the first example to "5678" and the user_guess to "5555". Run the code and observe the output. What does this tell you about how the 'correct digits' are counted?2
Add a third example using
secret_code_3 = "9012" and user_guess_3 = "0123". How many digits does your code report as correct? Why do you think this is the case?3
Adjust one of the examples to compare a 5-digit number instead of a 4-digit number. For instance, change
secret_code_2 to "98765" and user_guess_2 to "99775". Does the code still work correctly?Create
The Core ChallengeNow it's time to build your full code-cracking game! Use what you've learnt about comparing digits and combine it with random numbers, loops, and user input to create a complete game based on the secret agent scenario.
Success Criteria
Your program must generate a random four-digit number at the start of the game.
It must repeatedly ask the player to input their four-digit guess.
The game should continue looping until the player guesses the correct four-digit number.
After each unsuccessful guess, the program must tell the player how many digits they got correct and in the right position (without revealing which specific digits or positions).
Once the player guesses correctly, the game should congratulate them and state how many tries it took to crack the code.
Extension: Add an 'easy mode' where, after an incorrect guess, the program also reveals *which* digits were in the correct positions (e.g., by displaying
_ _ 3 _ if only the third digit was correct, or 1 _ _ 4 if the first and fourth digits were correct).Test Your Code
Run your code and check it against the expected behaviour below:
User Action
Expected Output
Purpose of Test
Input the correct secret code on your first attempt.
Your program should output a congratulatory message, stating it took "1 try".
Verifies the win condition and accurate try count.
Input a four-digit number with no digits matching the secret code (e.g., if secret is 1234, guess 5555).
The program should report: "You got 0 digits correct in their position."
Checks if the digit comparison correctly identifies zero matches.
Input a four-digit number with some digits matching the secret code, but not all (e.g., if secret is 1234, guess 1564).
The program should report "You got 2 digits correct in their position." (assuming 1 and 4 are correct).
Ensures partial match counting is accurate.
Play through the game, making several incorrect guesses before finally guessing the secret code.
The game should loop correctly, provide feedback after each incorrect guess, and then congratulate you with the final number of tries.
Confirms the main game loop, feedback mechanism, and final score reporting work as expected.
Don't forget to print your code and output to a PDF file and send it to your teacher.
Last modified: October 22nd, 2026
