A guessing game needs a condition that ends the loop. Planning that condition first makes the program easier to follow and prevents an accidental endless game.
Worked example
Choose a secret number and let the player enter guesses. After converting a guess to a number, compare it with the secret. A correct guess can break out of the loop. An incorrect guess can receive a hint before the next attempt. If you limit attempts, keep a counter and decide exactly when it increases.
What to check
Test a correct first guess and a sequence of incorrect guesses. Include invalid text input so that an error does not crash the program unexpectedly. Decide whether invalid input uses an attempt, then implement that rule consistently. You can begin with a fixed secret number during testing. Once the loop behaves correctly, adding a randomly chosen secret changes only one part of the game.
Continue with the book
Explore the full treatment, worked examples, and exercises in Code Quest with Python.
Companion article for Code Quest with Python, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
