Terrapin Resources
Getting User Input

READCHAR     READWORD     READLIST     READPROMPT     ALERT

This section shows you how to get and handle information from the user. You’ll need to use variables to get the most out of these commands. Variables allow you to store the information that the user enters and use it later. You’ll see how it works in the examples.

When you create a variable, you use MAKE and you start the variable name with a quotation mark, as in MAKE "MYNAME "HARRY.
When you want to use what the user typed, you get the information by using dots, not a quote mark, in front of the variable name, as in PRINT :MYNAME. Try the examples below to learn to use variables. See also the Creating Commands section, which also includes variables.


READCHAR
RC


Use READCHAR to capture a key that the user types.
You can assign it to a variable name to use that information later.
For more about creating procedures and variables, see the Creating Commands section.

TO KEYS
TYPE "|Please press a key: |
MAKE "THISKEY READCHAR
PRINT []
TYPE "|You pressed |
PRINT :THISKEY
END

Do you see how this procedure works? The key that the user presses is stored using the variable name "THISKEY. "THISKEY could have been any name, even "FIDO or "CUCUMBER. When you refer to the contents of the variable later, you use “dots” (a colon) instead of the quote mark. In this case, to refer to what the person typed, use :THISKEY.

You need to include the PRINT [] to move the text cursor down to the next line. This is because the TYPE command leaves the cursor after the last character. Read how PRINT and TYPE are different in the Listener Panel Text section.

» Things to Try

Did you try pressing all the keys on the keyboard?

The READCHAR reporter can even tell if the user pressed BACKSPACE, SPACE, ESC, or any other key.

You can write programs that use this information to make decisions.


READWORD
RW


READWORD reports the first word of what a person enters. If the person types “GEORGE WASHINGTON”, the result would be just GEORGE.

A word is a letter, a number, or a set of letters and numbers. A word ends with a space or the <Enter> key.

Here is an example of a procedure that uses READWORD.

TO GREET
PRINT [HELLO! IT'S NICE TO MEET YOU. WHAT IS YOUR NAME?]
MAKE "YOURNAME READWORD
(PRINT "HELLO, :YOURNAME)
END

Try answering the question with just your first name and then with your first and last name to see how READWORD works.

You need to put the second PRINT statement in parentheses because it has more than one input.

If the input that the user types is a number, you can use it to do math.

TO MATH
PRINT [PLEASE TYPE A NUMBER:]
MAKE "NUMBER1 READWORD
PRINT [THANK YOU. NOW TYPE ANOTHER NUMBER:]
MAKE "NUMBER2 READWORD
(PRINT :NUMBER1 "+ :NUMBER2 "= :NUMBER1 + :NUMBER2)
(PRINT :NUMBER1 "- :NUMBER2 "= :NUMBER1 - :NUMBER2)
(PRINT :NUMBER1 "X :NUMBER2 "= :NUMBER1 * :NUMBER2)
(PRINT :NUMBER1 "/ :NUMBER2 "= :NUMBER1 / :NUMBER2)
END

» Things to Try

Try creating procedures of your own to get user input in the form of a word.

Can you write a program that asks the user for several letters and makes a word out of them?
No, the program doesn’t need to create a real word, just put together the letters that the user typed.

What happens if you just press in the MATH program above without entering any numbers?

Think about how you might be able to prevent that from happening. (Remember NUMBER? in the Fun with Math section?)


READLIST
RL


READLIST works like READWORD except that it can accept more than one word as an input. It reports what the user types in the form of a list. In this version of GREET, you can type your full name!

TO GREET
PRINT [HELLO! IT'S NICE TO MEET YOU. WHAT IS YOUR NAME?]
MAKE "YOURNAME READLIST
(PRINT "HELLO, :YOURNAME)
END

The rest of the procedure is the same. The only change is that it uses READLIST instead of READWORD.

» Things to Try

Try writing different procedures that use READLIST.


READPROMPT message
(READPROMPT message preset-message)
(READPROMPT message preset-message TRUE/FALSE)
(READPROMPT)
RP message
(RP message preset-message)
(RP message preset-message TRUE/FALSE)
(RP)


Sometimes it is handy to get user input using a dialog box. READPROMPT lets you do just that.

READPROMPT can take from 0 to 3 inputs and reports the answer that the user enters as a single word, even if there are spaces in it.

Example 1

If you don’t give READPROMPT any inputs, it simply gives the user a place to type text. The prompt is always the same (but you can change it, as in Example 2). You would enter this command:

MAKE "ANSWER (READPROMPT)

The user’s answer is stored in the variable ANSWER. When you first create a variable, you begin with a single quotation mark, as in "ANSWER. Later, when you want to use the value stored in it, you use “dots” (a colon), as in :ANSWER.

PRINT :ANSWER

As is true for all these examples:
If the user clicks the Cancel button or presses the Esc key, READPROMPT reports FALSE.
If the user clicks the OK button without entering anything, READPROMPT reports an empty word, which looks like a blank line.

Example 2

If you use one input, its text will be the message, or prompt, that appears at the top of the dialog box.

In this example, the message is What is 3 + 3?

Then the user gets to type an answer. Their answer is stored in the variable ANSWER. When you first create a variable, you begin with a single quotation mark, as in "ANSWER. Later, when you want to use the value stored in it, you use “dots”, as in :ANSWER.

MAKE "ANSWER READPROMPT [WHAT IS 3 + 3?]
PRINT :ANSWER
6

Example 3

You can give READPROMPT a second input to automatically fill in the text field.
This is called a preset message because it is predefined by you, the programmer.
The user can then edit the answer before clicking OK.

MAKE "ANSWER (READPROMPT [HOW MUCH DO YOU LIKE LOGO ON A SCALE FROM 1 TO 5 STARS?] "*****)

Example 4

A third input to READPROMPT can be either TRUE or FALSE.
TRUE means to ignore the case of the letters and make them all uppercase.
FALSE means to keep the case of the letters exactly as the user typed them.

Compare this GREET procedure to the one above that uses READLIST. It is much shorter and keeps the case of the text.

TO GREET
MAKE "YOURNAME (READPROMPT "|Hello, what is your name?| [] FALSE)
(PRINT "|Hello,|" :YOURNAME)
END

» Things to Try

Try getting user input using READPROMPT with different prompts.

Think about how you could use READPROMPT in a program.


ALERT message
(ALERT message button1 button2 ...)


ALERT is a way to pop up a dialog box with choices for the user. It reports the user’s choice (the name of the button) as a word. You can then use the response to make decisions in your program.

ALERT takes as inputs a message and a variable number of answer choices, or buttons.

Example 1

If you just use ALERT with a message, the box has only an OK button for the user to click.

ALERT [LUNCH WILL BE SERVED AT 12:15 PM]
Result: OK

Example 2

This next example shows you how to create a series of alert boxes that the user can select from. The program could be a lot longer, couldn’t it?

MAKE "SELECTION (ALERT ["|What do you want for lunch?|] "|Sandwich| "|Soup| "|Salad| "|Yogurt and Toast|)
IF :SELECTION = "|Soup| THEN MAKE "SOUPCHOICE (ALERT [|What kind of soup do you want?|] "|Chicken Noodle| "|Vegetable and Rice| "|Minestrone| "|Tomato|) (ALERT (SENTENCE "|I hope you enjoy your| :SOUPCHOICE WORD :SELECTION ".))

Note that using WORD :SELECTION ". makes a word out of the selected item (soup, in this case), and the period at the end of the sentence. If you didn’t include WORD, there would be a space between soup and the period.

Note that putting the text inside the vertical bars (and starting with a quote), as in "|Chicken Noodle|, keeps the text’s upper and lower case.

Here is the series of three alerts you will see when you run this code:

In this program, if you don’t choose Soup, the program will stop.

Can you add to the program so that it handles any choice?

» Things to Try

Try using ALERT in a variety of ways. It’s fun to explore its many possibilities.