The INPUT command prompts the user for input and accepts user's input for a single result variable. By default, it sounds a beep to attract the user's attention when it requests input. If the nobeep keyword is present, the beep will not sound. (In either case, a beep will sound if the user enters a detectable error in response to the input request.)
The result variable may be either a string variable or a vector variable. The command opens an input panel just below the Output Window. The input panel displays the prompt and a text box in which the user can type a response. The prompt and the response are also printed to the Output Window as a log. After the user types his input and presses Return or Enter, the input panel disappears.
The user's input for a vector result can be a single number or a sequence of any of the patterns allowed to create vectors, such as (1 2 3), n,m or n#m, where n and m are numbers. Named constants and even variables defined prior to the INPUT command in the program may also be entered.
The user's input for a string result can consist of any printable keyboard characters.
The string keyword is used to indicate to Statistics101 that the user's input is to be interpreted as a string. If the result variable has not been previously defined, then the INPUT command interprets it to be a vector, unless the string keyword is present, in which case it becomes a string. If the result variable has been previously defined, either as a string or a vector, it will be taken to be whatever type it was originally defined as. If the string keyword is present but the result variable is previously defined to be a vector, that will produce an error message.
As an example, this program:
when the user inputs 1,5 4,1 will produce this output
vec: (1.0 2.0 3.0 4.0 5.0 4.0 3.0 2.0 1.0)
But adding the string keyword like this:
INPUT string vec
will produce this output:
If a literal prompt string or a variable prompt string (created by the STRING command) is present, then that string will be displayed as a prompt to the user. The literal prompt string must be enclosed in quotes. The user's response will be echoed immediately following the last character of the prompt, so if you want there to be a space between the last character of the prompt and the first character of the echoed response you'll need to include that space in the prompt.
When the prompt string is displayed, any occurrences of the character '%' are replaced by the name of the variable. For example,
INPUT "Enter the number of % in the family: " children
will display this prompt:
Enter the number of children in the family:
If there is no prompt string, then the default prompt will be used, where the variable name replaces the percent sign:
"Please enter %: "
If you want the prompt to display an actual percent sign, use two percent signs in a row (%%). Two consecutive percent signs will be displayed as a single percent sign and will not be replaced by the variable name. For example if the prompt string is:
"Please enter % as a %%: " rank
Which will produce the prompt:
Please enter rank as a %:
You can use the standard escape characters in the prompt string:
INPUT "Enter \"%\":\n " var1
The above prompt string will display the following prompt in the input window:
It will also echo the prompt in the Output Window, which because of the "\n " at the end of the prompt, will cause the response, which prints immediately after the last character of the prompt, to be printed indented by four spaces (assume the user entered 1,100):
For vector inputs, the user may enter a number or a list using any sequence of valid forms such as the following:
The entries will be concatenated into one vector and assigned to the result variable.
For string inputs, any input is allowed. The entry will be treated as a single string and assigned to the result variable.
Using HTML in the prompt string
You may also use some HTML markup tags in the prompt string. To do so, your prompt string must begin with lower case <html>. All your other HTML tags must also be lower case. The prompt will be displayed in the input window's prompt area as directed by the HTML tags. The Output Window cannot render HTML, so it will display the prompt in plain text. If you use the upper case HTML tags, the markups will be left in the prompt string as it is echoed to the Output Window.
Here is a simple prompt using HTML:
INPUT "<html>Enter a value for <i>%: </i>" var
This command will display the prompt with the name var in italics like this:
Enter a value for var:
The Output Window will display:
Enter a value for var:
Note that the HTML tags were removed and the "var: " is not in italics.
You can use any of the HTML markup tags from the following table in the prompt string, even making multi-line prompts. Just remember that if the markup takes a quoted argument, you must use a backslash escape character prior to the quote. Any quote mark that is not escaped will terminate the prompt string prematurely. See the highlighted escaped quotes in <font> and <p align> tag entries below.
Here is an example using most of the above tags in one wild and crazy prompt:
INPUT "<html><h1>Heading 1: Examples of HTML tags in prompt string</h1>normal label text <br><i>this should be italic</i><p>new paragraph</p><u>underlined line</u><br><strike>strikethrough</strike><br><small>small text</small><br><strong>strong emphasis</strong><br><hr><FONT face=\"Times New Roman\">Times New Roman font</font><center>Centered Text</center><p align=\"right\">this is a right-aligned paragraph</p>" var
This is all one command on one line. You can cut this line and paste it into Statistics101 to see the result. It will display in the prompt area as multiple lines, but in the Output Window area as a single line.
You can find references on the web that explain HTML tags. Here's one quick-reference site: http://www.web-source.net/html_codes_chart.htm
The following example demonstrates the use of the INPUT command to allow the user to select the number of trials on each run of the program:
' What is the probability of getting exactly two heads ' when tossing three coins? NAME heads tails COPY (heads tails) coin INPUT numberOfTrials 'Allow user to enter number of trials at runtime REPEAT numberOfTrials SAMPLE 3 coin toss COUNT toss =heads headCount SCORE headCount results END 'HISTOGRAM binsize 0.1 results COUNT results =2 successes ' count how many results were exactly 2 DIVIDE successes numberOfTrials probability ' probability = successes/numberOfTrials PRINT probability
When run several times, this program produced this output:
Please enter numberOfTrials: 100 probability: 0.43 Please enter numberOfTrials: 1000 probability: 0.392 Please enter numberOfTrials: 10000 probability: 0.369 Please enter numberOfTrials: 10000 probability: 0.3745
The following example program shows a use of the INPUT command to create a simple menu-driven program.
CLEAROUTPUT COPY 0 selection ' to force once through the outer loop PRINT "\n==================================" INPUT "Enter a vector: " var WHILE selection <> 5 ' loop on computation until user quits PRINT "------------------------------------" PRINT "1. Enter a new vector." PRINT "2. Compute mean." PRINT "3. Compute standard deviation." PRINT "4. Compute range." PRINT "5. Quit. COPY 1 keepGoing ' Force to go once through inner loop INPUT "Enter selection-> " selection IF selection = 1 PRINT "\n==================================" INPUT "Enter a vector: " var ELSEIF selection = 2 MEAN var result OUTPUT " The mean is: %12.4G\n" result ELSEIF selection = 3 STDEV var result OUTPUT " The standard deviation is: %12.4G\n" result ELSEIF selection = 4 MIN var varMin MAX var varMax OUTPUT " The range is: %12.4G to %12.4G\n" varMin varMax ELSEIF selection = 5 PRINT "Quitting." COPY 0 keepGoing ELSE PRINT "ERROR: selection must be between 1 and 5" END END
This will produce output that looks like this:
================================== Enter a vector: 100,199 ------------------------------------ 1. Enter a new vector. 2. Compute mean. 3. Compute standard deviation. 4. Compute range. 5. Quit. Enter selection-> 2 The mean is: 149.5
The above program can be rewritten to use the HTML prompt method like this:
CLEAROUTPUT COPY 0 selection ' to force once through the outer loop PRINT "\n==================================" INPUT "Enter a vector: " var WHILE selection <> 5 ' loop on computation until user quits
INPUT "<html><hr>1. Enter new vector.<br>2. Compute mean.<br>3. Compute standard deviation.<br>4. Compute range.<br>5. Quit.<br>Enter selection: " selection
IF selection = 1 PRINT "\n==================================" INPUT "Enter a vector: " var ELSEIF selection = 2 MEAN var result OUTPUT " The mean is: %12.4G\n" result ELSEIF selection = 3 STDEV var result OUTPUT " The standard deviation is: %12.4G\n" result ELSEIF selection = 4 MIN var varMin MAX var varMax OUTPUT " The range is: %12.4G to %12.4G\n" varMin varMax ELSEIF selection = 5 PRINT "Quitting." COPY 0 keepGoing ELSE PRINT "ERROR: selection must be between 1 and 5" END END