…
…
The computer picks a number from 1 to 100; code the bigger-smaller and hot-cold hints and the attempt limit with ready-made guesses; in the advanced step, let the computer play with binary search.
A classic game: the computer picks a secret number from 1 to 100, the player guesses, and the computer says "bigger" or "smaller". In this project you will build the game step by step and, at the end, make the computer find the number by itself in the fewest attempts.
Lessons used: variables, numbers and f-strings (2–5), conditions (6–8), algorithms, loops and accumulation patterns (9–13). The random lines that produce a random number are given ready (modules come in lesson 36). The last two steps are an optional advanced step.
Because the browser cannot read keyboard input, the player's guesses are in a ready-made list. Randomness is fixed with random.seed, so the secret number comes out the same each time you run the code; this lets us check the result.
What you'll do
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The lines that pick the game's secret number are ready. The random module produces random numbers; you will see modules in detail in lesson 36, but for now it is enough to know this: random.randint(1, 100) gives an integer from 1 to 100 (both included), and random.seed(2026) makes the same number come up every time you run the code. This way we can check the result; in a real game you would delete the seed line. The "cheat mode" line lets you see the secret number while you develop.
A guess gives one of three results: if it is smaller than the secret number, Say a bigger number; if it is larger, Say a smaller number; if it is equal, You got it!. Use if / elif / else to print the message for guess. Check also runs your code with other guesses (for example 16 and 5).
This step's code cell is for members.Sign up free
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
forbreakEach line of the output should look like 1. guess: 50 → smaller.
This step's code cell is for members.Sign up free
Make the game harder: the player has only 5 attempts. Now the loop has two reasons to stop: the player found the number, or the attempts ran out.
Keep track of whether the player found it in a flag called found (lesson 12): it starts as False, becomes True on the correct guess, and the loop is left. On a wrong guess, print the hint and the attempts left; when the number of attempts reaches the limit, leave the loop too. After the loop, the lines that print You win! or You lose! The number was 16. depending on the flag are ready.
This step's code cell is for members.Sign up free
Saying "bigger" is not enough; the player also wants to know how close they got. Look at the size of the difference between the guess and the secret number: abs (a built-in function you saw in lesson 5) gives the difference without its sign; abs(10 - 16) and abs(22 - 16) are both 6.
| Difference | Hint |
|---|---|
| 0 | You got it! |
| 1–3 | very hot |
| 4–10 | hot |
| 11–25 | warm |
| 26 and more | cold |
In the elif chain, write the thresholds from small to large (lesson 7): the first true condition runs. For each guess, print <guess>: <direction>, <hint>; the direction is bigger or smaller. On the correct guess, print <guess>: You got it! and leave the loop.
This step's code cell is for members.Sign up free
*Optional advanced step.* Now let the computer make the guesses. The best strategy is to say the exact middle of the remaining range; the name of this method is binary search. Start with low = 1, high = 100 and find the middle with (low + high) // 2 (// is integer division, lesson 3). If the guess is too small, the lower bound becomes guess + 1; if it is too big, the upper bound becomes guess - 1.
Because it is not known in advance how many rounds it will take, loop with while True and leave with break when you find it (lesson 11). Because the range is halved at each step, every number from 1 to 100 is found in at most 7 attempts.
This step's code cell is for members.Sign up free
*Optional advanced step.* Let the computer play three rounds; in each round it picks a different secret number with a different seed. The two lines that set the seed and pick the number are ready. The outer loop goes over the rounds, the inner loop runs the binary search (nested loops, lesson 13). In each round low, high and attempt must start again before the inner loop; the total starts outside both loops.
At the end, print the average number of attempts per round with one decimal place. In this cell we use the name number for the secret number; secret from the earlier cells should not change.
This step's code cell is for members.Sign up free