Skip to content

Ask, then use the answer

Let someone supply the words, then turn their answers into a sentence.

Our greeting gets its name from a line we wrote. Let’s ask the person running it instead. Then we can ask for more words and surprise them with a sentence.

Continue with the greeting from Remember a name; keep the program. If you are joining here, enter the complete listing below. Numbered lines are stored instructions; R at an empty input line enters RUN. Make the Spectrum answer explains keyword entry and editing.

Ask for a name

Replace line 10. Lines 15 and 20 keep their jobs:

10 INPUT "Your name ";n$
15 PRINT "Hello, ";n$
20 PRINT "from the Spectrum"

After typing 10, press I to enter INPUT. In Host Keyboard mode, type the quotes, semicolon and $ with your usual host keys. The window title shows the mode; choose Machine → Keyboard → Host Keyboard if needed.

Run it. The prompt Your name appears near the bottom of the screen, with a pair of quotation marks. Type Sam between the quotes already supplied, then press Enter. Do not type another pair. The greeting appears above.

INPUT displays the prompt, waits for an answer, then stores it in n$. It does the assignment itself: we do not need a separate LET line. Line 15 reads the value when execution reaches it.

Run it again and answer Ada. The listing stays the same; the stored answer and greeting change. We can now use the program without editing it.

Foundations: Asking the Player explores this exchange with ASK … INTO in pseudocode. On the Spectrum we write INPUT; the idea of waiting for an answer and keeping it is the same.

Give the words somewhere to go

We’ll ask for a describing word, such as sleepy, and an animal, such as dragon. Add lines 30, 40 and 50:

10 INPUT "Your name ";n$
15 PRINT "Hello, ";n$
20 PRINT "from the Spectrum"
30 INPUT "Describing word ";a$
40 INPUT "Animal ";b$
50 PRINT n$;" met the ";a$;" ";b$

Enter one answer at each prompt, pressing Enter after each. Try Sam, sleepy and dragon.

The Spectrum displays Hello, Sam, from the Spectrum, and Sam met the sleepy dragon.
Three answers become one sentence. The program supplies the connecting words.

The variables have separate jobs: n$ holds the name, a$ the describing word and b$ the animal. Those letters are our choices. BASIC does not know that b$ is meant to contain an animal.

Line 50 prints five pieces: the name, " met the ", the describing word, a space, and the animal. Semicolons make the pieces follow immediately; the spaces inside the quotes keep the words apart. There is no semicolon at the end, so the next PRINT would begin on a new line.

What sentence will Jo, enormous and owl produce?

Jo met the enormous owl. The word the works before either sleepy or enormous, so we do not need a rule to choose between “a” and “an”. Try these answers to check the prediction.

Try awkward answers

Run the program and press Enter at each prompt without adding any text between the quotes. An empty string contains no characters. The program accepts it: the greeting has no name, and the sentence has gaps where the answers would go. That is allowed by BASIC, though it makes a poor story.

Now try a longer set: Alexandertheexplorer, extraordinarily, hippopotamus. The sentence crosses the screen edge. The Spectrum’s normal text display has 32 columns; output continues on the next row, even through the middle of a word. The answer has not been shortened in memory.

For this version, ask for short answers: a name, one describing word and one animal. These are instructions for the person using the toy, not limits enforced by our code. Later, comparisons and repeated questions will let us reject an answer or ask again.

Let someone else try it. If they cannot tell what to enter, improve the prompt. For example, change Describing word to Word like sleepy . Keep the space at the end. Clear prompts are part of designing the interaction, just as much as the sentence is.

Next, A story worth sharing gives this meeting a setting and another event, and arranges the result for reading.

Sources

Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapter 2, pp. 17–18 (PRINT separators and the quotes supplied for string input); chapter 15, “More about PRINT and INPUT” (text layout and input prompts).