Give a useful clue
Compare a guess with a known secret, then give a clue that helps someone choose again.
“Wrong” tells us that a guess failed. “Too low” tells us something we can use. We’ll begin Lucky Number by making the Spectrum choose a useful response.
This is a new program. If Story Builder is still in memory, save and export it first using A story worth sharing. Then restart Emu198x so that the Sinclair copyright message appears. Otherwise, old numbered lines may become part of our new program.
Use the 48K setup and Host Keyboard mode.
If you are joining here, numbered lines store instructions; pressing R at an
empty input line enters RUN. Make the Spectrum answer
explains entering and changing those lines.
Recognise the right answer
Our secret is 7, in a range from 1 to 10, including both ends. Knowing the answer helps us check the program. We will let the computer choose a new secret once the clues work.
Enter this listing. The instructions below explain the new keyword pair in line 40.
10 LET secret=7
20 PRINT "Guess a whole number 1 to 10"
30 INPUT "Your guess ";g
40 IF g=secret THEN PRINT "Correct!"
After a line number, L enters LET, P enters PRINT and I enters INPUT.
Line 10 stores 7 in secret. A numeric variable has no $; its name can contain
several letters. g will hold the guess. Line 20 tells the player what to enter.
For line 40:
- Type
40, press U forIF, then typeg=secret. - Hold left Option/Alt, press G to enter
THEN, then release both keys. - Press P for
PRINT, type"Correct!", then press Enter.
In Host Keyboard mode, left Option/Alt acts as SYMBOL SHIFT for deliberate Spectrum
keyword chords. THEN is entered with left Option/Alt+G, not four ordinary letters. Release
left Option/Alt to resume normal typing. After THEN, the cursor expects another command,
so P enters PRINT again. Use the Option/Alt key on the left of the
keyboard; right Option/Alt remains available for your host layout’s characters.
Run the program. At Your guess, type 7 and press Enter. Numeric INPUT
does not supply quotation marks: enter the number on its own. The answer is
stored in g, and Correct! appears above the input area.
IF g=secret THEN PRINT "Correct!" asks whether the two numbers are equal.
A question with a true or false result is a condition. When this condition
is true, BASIC carries out the instruction after THEN. When it is false,
BASIC skips the rest of that numbered line.
The two equals signs have different jobs: LET secret=7 stores a value;
IF g=secret compares two values. The comparison does not change either one.
Run again with 4. This time there is no response beyond BASIC’s 0 OK
report. We have described what to do for a correct guess, but nothing else.
Say which way to look
Add line 50. Enter IF and THEN as above; in Host Keyboard mode, type
< with your usual punctuation key.
10 LET secret=7
20 PRINT "Guess a whole number 1 to 10"
30 INPUT "Your guess ";g
40 IF g=secret THEN PRINT "Correct!"
50 IF g<secret THEN PRINT "Too low"
< means less than. g<secret asks whether the guess is smaller than the
secret. Run with 4 again: now we get Too low. A player can rule out 4 and
everything below it, leaving 5 to 10 as possibilities.
Try 9. Neither condition is true, so the program still gives no clue for
that case. Add line 60; > means greater than:
10 LET secret=7
20 PRINT "Guess a whole number 1 to 10"
30 INPUT "Your guess ";g
40 IF g=secret THEN PRINT "Correct!"
50 IF g<secret THEN PRINT "Too low"
60 IF g>secret THEN PRINT "Too high"
Run separately with 4, 7 and 9. Expect Too low, Correct! and
Too high, respectively.

BASIC tests lines 40, 50 and 60 in order. A true condition does not prevent later lines from being tested. We get exactly one response because a number cannot be equal to, lower than and higher than the same secret at once.
Stock Sinclair BASIC has no ELSE clause. These separate tests express the
three possibilities. Foundations: Making a Choice
explores the same comparisons in pseudocode; its SHOW corresponds to our
PRINT, not a command to type here.
With a secret of 7 and a guess of 7, which lines are tested?
All three: 40, 50 and 60. Only line 40 prints anything. The other comparisons
are false, so their PRINT instructions are skipped.
A number is not necessarily a useful guess
Try 11, then 6.5 in separate runs. Both are numbers BASIC can compare, so both receive clues. Our instructions ask for a whole number from 1 to 10, but the code does not yet enforce that rule.
Numeric INPUT can also evaluate an expression such as 3+4. That produces
7. It is asking for a numeric value, rather than checking that every character
you typed was a digit. Entering a word does not make it a string answer: BASIC
tries to interpret it as part of a numeric expression and may give an input or
variable report. That is different from our program rejecting an out-of-range
number. If a report ends the run, enter RUN again. If the input stays on screen
with an error marker, correct that input and press Enter. Use numbers for
these checks.
Change the secret in line 10 to 2 and predict the responses to 1, 2 and 3. Try them, then restore the secret to 7 for the next unit. Keeping test values known makes a missing or reversed clue easier to find.
Next, Keep guessing will return to the question after a wrong guess, count attempts and enforce our stated range. Keep this program in memory to continue.
Sources
Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming,
second edition (Sinclair Research, 1983), chapter 1 (keyboard modes), chapter 2
(numeric input and variables), and chapter 3, pp. 25–26 (comparisons and IF … THEN).