This tutorial covers:
- Input statements
- Keywords ‘input' & ‘clear'
Before starting, you should be familiar with:
- The BoGL basics
- Understanding Expressions
- Locally Defined Values
- Conditional Expressions (If/Else)
At the end, you should be able to:
- Use the ‘input' and ‘clear' keywords
- Illustrate how inputs work
By this point, you already know that you can interact with your BoGL programs by using the interpreter, and that you can provide input to your functions by specifying their arguments. In addition to this, there is yet another way to make your programs interactive and allow user input. In this tutorial we will be going over the input statement.
input acts as a placeholder that will be replaced with a value provided by the user during runtime. You can use this inside a program to enable it to pause for input, then resume after that input is recieved. This is helpful for making programs that allow users to make multiple decisions (such as a game that requires a player to take multiple turns).
input keyword is evaluated, the interpreter will prompt the user to provide input to the program (giving the message seen below).
🤖 BoGL Says: Enter input, or "clear" to stop.
The program will not proceed until the user has typed something into the interpreter. The prompt expects you to type in a value that matches the
Input type, which you should define if you are using the input statement in your program. An input that does not match the
Input type will be rejected by the interpreter (see message below).
Got type Bool, but expected type Int from input. Your last input was discarded. Please enter a new one.
To define the input type, you can create a type definition as you normally would, but name the type
If the word
clear is given as input, the program will abort.
Example: Add Two Numbers
The program below will prompt the user for input twice. The resulting value will be the sum of those two inputs.
Excercise: Second Power
Write the definition for a value that takes a single integer input from the user during runtime, and then returns the integer that was input multiplied by itself.
Click to see a hint
You will have to use a local value in your defining expression.
Click to see a solution (but try yourself first!)
Example: Rock Paper Scissors
Below is an implementation of the game Rock Paper Scissors in BoGL. Calling the value
playGame will cause the interpreter to ask for user input twice, each input representing a player move. These input player moves are stored as local values (
p2move) which are then compared to see which player won the game.