← ACARA Digital Technologies Years 7–8

Number guessing game with a loop, decisions and a counter

The loop at the heart of a guessing game. It keeps asking until the guess matches the random secret number. If/elif/else gives hints and a counter adds 1 each turn.

Understanding Number guessing game with a loop, decisions and a counter in ACARA Digital Technologies Years 7–8

What it does: This line runs once every time round the loop, so guesses goes up by 1 for every guess the player makes. The comparison guess > secret is True when the guess is too big, so the program prints a hint to guess lower. random.randint(1, 100) picks a whole number from 1 to 100. You also need import random at the top of the program. The loop must run while the guess is NOT equal to the secret. With == the loop would stop straight away unless the first guess was right. A single = stores a new value in the variable. == only compares two values, so the counter would never change.

Common mistakes: random is the module, not a function. The function that picks a whole number in a range is random.randint(1, 10). Python uses the import keyword, not include. It should be import random, otherwise random.randint will not work.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 522–560) and word search (Modules 522–580).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line makes the computer pick a secret number from 1 to 100?
secret = random.randint(1, 100)secret = random(1, 100)
Matches
if guess = secret: print("Correct!")
↓
A single = assigns a value to a variable. To compare two values you need the double equals == operator.
Applying
Meanings
Term
while guess != secret:
Meaning
The loop at the heart of a guessing game. It keeps asking until the guess matches the random secret number. If/elif/else gives hints and a counter adds 1 each turn.
Practice
import random
Testing
Gaps
secret = random.(1, 10)
Check Answer
Spots
1import random
2secret = random(1, 10)

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Elephant' badge — OOP Heavyweight, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line makes the computer pick a secret number from 1 to 100?
A. secret = random.randint(1, 100) ✓
B. secret = random(1, 100)
random.randint(1, 100) picks a whole number from 1 to 100. You also need import random at the top of the program.
2. Which loop keeps a guessing game running until the player is right?
A. while guess == secret:
B. while guess != secret: ✓
The loop must run while the guess is NOT equal to the secret. With == the loop would stop straight away unless the first guess was right.
3. Where should the counter start before the loop begins?
A. guesses = secret
B. guesses = 0 ✓
The counter starts at 0 because no guesses have been made yet. Then you add 1 each time round the loop.
4. Which line counts one more guess each turn of the loop?
A. guesses = guesses + 1 ✓
B. guesses == guesses + 1
A single = stores a new value in the variable. == only compares two values, so the counter would never change.
5. The player guesses 40 and the secret is 25. Which hint is correct?
A. Too high ✓
B. Too low
40 is bigger than 25, so the guess is too high. The code for this is if guess > secret:

Match the Pairs

secret = random.randint(1, 20) → Picks a random whole number from 1 to 20 and stores it as the secret number the player must guess
while guess != secret: → Keeps repeating the indented lines until the player's guess is equal to the secret number
Count each turn by adding 1 to the guesses counter inside the loop → guesses = guesses + 1
Give a hint when the guess is bigger than the secret number → if guess > secret: print("Too high!")
if guess = secret: print("Correct!") → A single = assigns a value to a variable. To compare two values you need the double equals == operator.
secret = random.randint(1, 20) while guess != secret: guesses = 0 → The counter is reset to 0 on every turn of the loop, so it can never count up. It should start at 0 before the loop and add 1 inside it.

Key Term

while guess != secret:
The loop at the heart of a guessing game. It keeps asking until the guess matches the random secret number. If/elif/else gives hints and a counter adds 1 each turn.

Fill in the Blanks

1. import random secret = random.randint(1, 10)
Hint: Type the random function that picks a whole number between two limits.
2. secret = 7 guess = 0 while guess != secret:
Hint: Type the comparison operator that keeps the loop running while the guess is wrong.
3. while guess != secret: guess = int(input("Guess: ")) count += 1
Hint: Type the operator that adds to the counter each turn.
4. if guess < secret: print("Too low!") elif guess > secret:
Hint: Type the keyword that checks a second condition after the if.
5. print("Too high!") else: print("Correct!")
Hint: Type the keyword that handles every other case when no condition above matched.

Spot the Error

Question 1
A import random
B secret = random(1, 10)
C guess = 0
D while guess != secret:
E guess = int(input("? "))
Line B — random is the module, not a function. The function that picks a whole number in a range is random.randint(1, 10).
Question 2
A include random
B secret = random.randint(1, 20)
C guess = int(input("Guess: "))
D print("Your guess:", guess)
Line A — Python uses the import keyword, not include. It should be import random, otherwise random.randint will not work.
Question 3
A count == 0
B secret = 7
C guess = 0
D while guess != secret:
E guess = int(input("? "))
F count = count + 1
Line A — == compares values, it does not store one. To start the counter at zero, use a single = : count = 0.
Question 4
A while guess != secret:
B guess = int(input("? "))
C if guess > secret
D print("Too high!")
E elif guess < secret:
F print("Too low!")
Line C — The if line is missing its colon at the end. It should be if guess > secret:
Question 5
A guess = 0
B while guess = secret:
C guess = int(input("? "))
D if guess == secret:
E print("Correct!")
Line B — A single = assigns a value, so it can't be used as a loop test. The loop should keep going while the guess is not the secret: while guess != secret:

Student with a class code? Join your class →