Number guessing game algorithm

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Number guessing game algorithm

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop line keeps a number guessing game going until the player wins?
A REPEAT 1 TIME
B REPEAT UNTIL guess = secret
2. The secret number is 50 and the player guesses 70. What should the algorithm say?
A Too low
B Too high
3. The secret number is 32 and the player guesses 18. What should the algorithm say?
A Too low
B Too high
4. Which set of decisions covers every possible guess?
A IF guess > secret THEN too high, ELSE IF guess < secret THEN too low, ELSE correct
B IF guess > secret THEN too high, ELSE IF guess > secret THEN too low, ELSE correct
5. Where should 'add 1 to count' go to count every guess?
A Inside the loop, once for each guess
B After the loop, just once

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1REPEAT UNTIL guess = secret AIF guess < secret THEN SAY "Too low"
2IF guess > secret THEN SAY "Too high" BKeep asking for guesses again and again until the guess matches the secret number.
3Count every guess by adding 1 to the counter each time round the loop. CThe loop stops when the guess is too high, not when it is correct. It should be REPEAT UNTIL guess = secret.
4The decision for a wrong guess that is smaller than the secret number gives a 'too low' clue. Dcount ← count + 1
5REPEAT UNTIL guess > secret EThe counter is reset to 0 on every go round the loop, so it never counts above 1. Set count to 0 once before the loop starts.
6SET count ← 0 (placed inside the loop) FIf the guess is bigger than the secret number, tell the player the guess is too high.

Definitions

REPEAT UNTIL guess = secret

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
SET secret ← 7 SET count ← _ REPEAT UNTIL guess = secret
2.
REPEAT _____ guess = secret INPUT guess SET count ← count + 1
3.
IF guess _ secret THEN OUTPUT "Too low"
4.
INPUT guess SET count ← count _ 1
5.
IF guess = secret THEN OUTPUT "Correct! Guesses: ", _____

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A SET count TO 1
B REPEAT UNTIL guess = secret
C INPUT guess
D ADD 1 TO count
Question 2
A SET secret TO 7
B SET count TO 0
C REPEAT UNTIL guess = secret
D INPUT secret
E ADD 1 TO count
Question 3
A SET count TO 0
B REPEAT UNTIL guess ≠ secret
C INPUT guess
D ADD 1 TO count
Question 4
A INPUT guess
B IF guess < secret THEN
C OUTPUT "Too high"
D ENDIF
Question 5
A REPEAT UNTIL guess = secret
B INPUT guess
C IF guess < secret
D OUTPUT "Too low"
E ADD 1 TO count

Answer Key

Number guessing game algorithm
Section A: Quick Questions
Q1
B — REPEAT UNTIL guess = secret
Q2
B — Too high
Q3
A — Too low
Q4
A — IF guess > secret THEN too high, ELSE IF guess < secret THEN too low, ELSE correct
Q5
A — Inside the loop, once for each guess
Section B: Match the Pairs
Q1
REPEAT UNTIL guess = secret → Keep asking for guesses again and again until the guess matches the secret number.
Q2
IF guess > secret THEN SAY "Too high" → If the guess is bigger than the secret number, tell the player the guess is too high.
Q3
Count every guess by adding 1 to the counter each time round the loop. → count ← count + 1
Q4
The decision for a wrong guess that is smaller than the secret number gives a 'too low' clue. → IF guess < secret THEN SAY "Too low"
Q5
REPEAT UNTIL guess > secret → The loop stops when the guess is too high, not when it is correct. It should be REPEAT UNTIL guess = secret.
Q6
SET count ← 0 (placed inside the loop) → The counter is reset to 0 on every go round the loop, so it never counts above 1. Set count to 0 once before the loop starts.
Section C: Definitions
Q1
REPEAT UNTIL guess = secret — The guessing game loop: keep asking for a guess until it matches the secret number. Inside, decide too high, too low or correct, and add 1 to the count each time.
Section D: Fill in the Blanks
Q1
SET secret ← 7 SET count ← 0 REPEAT UNTIL guess = secret
Q2
REPEAT UNTIL guess = secret INPUT guess SET count ← count + 1
Q3
IF guess < secret THEN OUTPUT "Too low"
Q4
INPUT guess SET count ← count + 1
Q5
IF guess = secret THEN OUTPUT "Correct! Guesses: ", count
Section E: Spot the Error
Q1
A — The count must start at 0 because no guesses have been made yet. Starting at 1 makes the total one too many. It should be SET count TO 0.
Q2
D — The player types a guess, not the secret number. Typing into secret would change the number being guessed. It should be INPUT guess.
Q3
B — The game must keep going until the guess is the same as the secret. With ≠ the loop would stop on a wrong guess. It should be REPEAT UNTIL guess = secret.
Q4
B — The message says Too high, so the condition must check for a guess bigger than the secret. It should be IF guess > secret THEN.
Q5
C — An IF decision needs the word THEN after the condition. It should be IF guess < secret THEN.