User Input
Programs can be interesting even if they don't interact with the user, but people often get tired of them pretty quickly. Disc language provides two ways of accepting user input. We will take a look at both of them on this page, and use them in small programs. This information will be useful as example code and tutorials get more complicated.
Prompting
Prompting the user is how you can collect typed answers from the user. In the browser, you will see a prompt dialog pop up, which will allow users to enter information from the keyboard.
We can enhance our original "Hello, World" program with a prompt. In this example, we will add a prompt to get the user's name, and then greet them directly. Let's take a look.
begin
call clearScreen
let name be prompt: "What is your name?"
let message be join: "Hello, " name "!"
print: message
end
Read this code aloud and think about what it does. We have some new pieces to understand. Let's have a look at each of them and what they do for the program.
Once you think you get it, head over to the create something area, and try writing it yourself!
let ... be ...
The "let ... be ..." construct is known as variable initialization. That's a fancy way of saying we're giving some value a name so we can refer to it later. The grammar in this code works like this:
- let -- this says we are going to create a new variable
- ... (name) -- the first set of ellipses is where the variable name goes
- be -- this is referred to as assignment. This means we are assigning a value to the name we gave the variable.
- ... (value) -- This is the value assigned to our new variable.
Join
The join
function takes several values and sticks them all together as a single string. This behavior is known as concatenation. In this program we are using join
to combine the hello message and the name we received from the user.
readKey
The readKey
function is a way to capture single key strokes from a user. This can be especially useful for menus and other quick interactions with the user. Something that is common is the "press any key to exit...". This can be implemented with the readKey
function.
begin
call clearScreen
let name be prompt: "What is your name?"
let message be join: "Hello, " name "!"
print: message
print: ""
print: "Press any key to exit..."
call readKey
call clearScreen
end
The program above is basically a little extra behavior on top of our "Hello World" program. This will do the same thing our program above did, plus it will wait until the user presses a key before it completes running. There are other uses as well:
begin
print: "****************************"
print: "* My Program Splash Screen *"
print: "****************************"
print: ""
print: "Press any key to continue..."
call readKey
end
The code above will print out a splash screen, and then wait for the user to press a key before continuing. If you run the "To Do List", "Game of Life", or "Rule 110" programs, you will see a splash screen like this. I opted for a countdown timer, but the readKey
function would also work quite well.
The readKey
function also returns the key pressed by the user. The following code will print any key the user presses:
begin
call clearScreen
print: "Press a key: "
let pressedKey be call readKey
print: ""
print: join: "You pressed: " pressedKey
end
In the end, user input can be used to add interactivity to your programs, and increase engagement in your program. Really, user input gives you a broader way to get creative. The rest of the program is up to you!
Next Steps In Your Program
The ideas presented in this section are other ways to think about the code you write. Some of the ideas here may not feel like a better approach, and that's okay. Ideally what you will discover is, there is always another way to solve the same problem.
Combining Functions
When you are feeling confident with each piece of this program, try combining functions together to get the same behavior. Though it is not always advisable to combine lots of complicated behavior together on a single line, sometimes the program makes more sense when functions are combined in useful ways. The following examples are different ways of writing the same program. Read them aloud and see which makes the most sense to you!
begin
let name be prompt: "What is your name?"
# we moved message "inline" eliminating the variable
print: join: "Hello, " name "!"
end
begin
# Here, the entire program is written on one line.
print: join: "Hello, " (prompt: "What is your name?") "!"
end