Skip to content
Game 0 Unit 4 of 15 1 hr learning time

LET, and What You May Name

Store a value in a named box with LET, use it, and change it — then meet the Spectrum's own rule: numeric names can be long, but a text variable must be a single letter plus a dollar sign. See name$ fail, and learn why.

27% of Meet BASIC

You met the idea of a variable in General Programming — a named box that remembers a value. In Sinclair BASIC the keyword is LET, and the Spectrum has its own rule about what you may call a box. That rule trips nearly everyone once; we'll trip it on purpose so it never surprises you.

Milestone 1 — store a value, then use it

  10 LET score = 100
  20 LET n$ = "Sam"
  30 PRINT n$; " scored "; score

LET score = 100 puts 100 in a box named score. LET n$ = "Sam" puts the text Sam in a box named n$. Line 30 reads both back. RUN it: Sam scored 100.

The Spectrum screen showing Sam scored 100, with the report 0 OK, 30:1.
Two boxes — a number in score, text in n$ — read back into one line. The = in LET means 'put this value in the box', not a question.

The = in LET stores: "put this value in the box." It isn't asking whether two things are equal — that's a different job, for Unit 6.

Milestone 2 — change what's in the box

A box remembers until you change it. Add two lines that update score:

Step 2: change score, and read it again
+2
11 10 LET score = 100
22 20 LET n$ = "Sam"
33 30 PRINT n$; " scored "; score
4+ 40 LET score = score + 50
5+ 50 PRINT n$; " now has "; score
46

LET score = score + 50 reads the box, adds 50, and puts the result back in the same box. Run it: score goes from 100 to 150.

The Spectrum screen showing Sam scored 100 and Sam now has 150, with the report 0 OK, 50:1.
The same box, changed: score holds 100, then 150. Reading a box, working on the value, and storing it back is how a score, a life count, or a position moves.

Milestone 3 — the naming rule, shown failing

Here's the rule. Numeric names can be longscore, lives, hits are all fine. But a text variable must be a single letter plus $n$, a$, x$. A longer text name like name$ is not allowed. Watch what happens when you try:

  10 LET name$ = "Sam"
  20 PRINT name$
The Spectrum screen showing the report C Nonsense in BASIC, 10:1 near the bottom.
name$ stops the program with 'Nonsense in BASIC'. The Spectrum read 'name' as a long numeric name — which is allowed — and then the dollar sign made no sense after it.

That report is worth understanding. The Spectrum read name as a numeric variable name — long numeric names are fine — and then hit the $, which makes no sense stuck on the end of a number's name. So it stops with Nonsense in BASIC. The fix is to make the text box a single letter: n$, not name$. (Other home computers have their own rules — a C64 keeps only the first two letters of any name — which is the point: the idea of a named box is universal, but the naming rules are local.)

When it doesn't work

  • Nonsense in BASIC on a LET line. Usually a text variable with a long name. Shorten it to a single letter plus $n$.
  • Variable not found. You read a box you never stored into — often a typo, so the name you read doesn't match the name you set. Check the spelling on both lines.
  • A name did nothing useful. You may have used a BASIC keyword as a name. Keywords are reserved; pick a plain word the Spectrum doesn't already own.

Before and after

You started knowing what a variable is and finished writing them in Sinclair BASIC — storing, using, and changing a box — and you met the rule that catches everyone: text names are a single letter plus $. The idea underneath: LET stores a value in a named box; numeric names can be long, text names cannot.

Try this

  • A second number. Add LET lives = 3 and print it beside the score.
  • Fix the broken one. Change name$ to n$ in Step 3 and run it — the same program works the moment the name is legal.
  • Change twice. Add 50 to score a second time. Predict the total before you run it.

What you've learnt

  • LET stores a value in a named box; the = means "store", not "is equal to".
  • Numeric names can be long (score); text names must be a single letter plus $ (n$).
  • LET score = score + 50 reads a box and stores a new value back in it.
  • A too-long text name stops with Nonsense in BASIC — the Spectrum read the word as a number's name, then the $ made no sense.

What's next

Your boxes hold values you typed into the program. In Unit 5 the player fills them: INPUT reads what someone types at the keyboard into a variable — the first time the program waits for you.