Indentation🔗
"Indentation" is when one or more tabs (i.e. indents) are placed at the beginning of a line to offset it from the lines before it. Note that indentation must be created using tabs. If you use spaces instead to achieve the indents, the thing you're trying to do won't work, even though the code itself will look the same to the naked eye (a tab looks like a bunch of spaces, but it isn't the same thing in GT).
GT programs use indentation for a special purpose. If a GT line is indented so that it starts to the right of the GT line that's just above it, it means that the indented line "is part of" or "belongs to" the line above it. For instance:
*question: What's your least favorite number?
*type: number
When these two lines are run, the *question
keyword displays a question ("What's your least favorite number?") to the user. There is also a text box that the user can type their answer into. But because the type of the question (determined by the *type
keyword) is "number," the user is only able to put a number in the text box (the user won't be able to submit their answer unless it's a number). The fact that *type
is indented beneath *question
indicates that the *type
keyword is part of / belongs to the question.
On the other hand, consider the following program:
*question: What's your least favorite number?
*type: number
The result would not be the same as the GT program we just saw above. This program would give an error because *type
doesn't make sense when it is not part of a question, and without the indentation, GT doesn't realize that *type
is referring to the question. As you can see, your program's behavior can change a lot based on the indentation used on each line.
Let's look at another common usage of indentation. Consider the following program, which asks a multiple choice question to the user:
*question: What's better, ham or butter?
Ham
Butter
It's all the same to me.
In this case, the question "What's better, ham or butter?" will be presented to the user, and they will be given the choice between three options which they can select ("Ham," "Butter" and "It's all the same to me.") The user will have to select one (and only one) of these options, at which point the program will continue on.
Keywords can also have comments and multiple keywords indented beneath them, which alter their behavior. Take a look at this example:
*question: How many marbles are in the bag?
--later, let's change this to something besides marbles.
*type: number
Here, *question
has two lines indented beneath it. The comment, which begins with "--" is just a note and doesn't actually do anything when the program is run. The keyword *type
has the option "number," so the user's response must be a number.
Different types of keywords require and/or allow different things to be indented beneath them. For the *question
keyword, you can indent a *type
keyword beneath it in order to specify the type of this question, though this is not required. But trying to indent *fish
beneath *question
would produce an error, because *fish
is not a valid keyword!
Complex Indentation🔗
Indentation can be more complex than the examples we've just seen. For instance, you can use multiple levels of indentation. That is, indented lines can themselves have lines indented beneath them. To finish up this section, let's now look at a larger example of indentation:
Hi there!
*question: What's your favorite color?
Chartreuse
Yuck
Mauve
Awful
Taupe
What's taupe?
Some other stupid color.
That's nice! My favorite color is clear.
--this is a comment, it's really just a note.
*question: If you had dogs, how many would you have?
*type: number
That's a stupid number of dogs to have.
When run, the program does the following:
- It displays the text "Hi there!"
- It also (at the same time) displays a question to the user ("What's your favorite color?"), which appears beneath the text "Hi there!"
- The user is additionally presented with four options they can choose among.
- Once the user picks one of these options, the program then displays to the user a particular line of text, depending on which answer they choose.
- At the same time (appearing immediately after the text) another question is displayed to the user ("If you had dogs, how many would you have?") and the user is presented with a text box in which they enter a response, which has to be a number.
- Finally, the text "That's a stupid number of dogs to have." is displayed to the user.
Don't forget that there are some keywords that work on their own, and some keywords that actually modify the behavior of another keyword. GuidedTrack won't recognize the latter as valid if you don't indent them under an appropriate keyword for them to modify (*type
for *question
, as in the above example)!
Also keep in mind that some keywords (like *if
, and *while
) may have any non-modifying keyword indented beneath them (for example, a *question
keyword can be indented beneath an *if
keyword). In these cases though, the indented keywords are only executed if the "owning" keyword's conditions are met, but not otherwise. For example, if you want a question to only appear for users who are above 18, you might have a line like *if: age > 18
, with a risque question indented beneath that. This question will only pop up though if the age condition is met first.
Keep these things in mind, and you'll be comfortable working with indented commands in no time!
Indenting multiple lines at once🔗
Need to indent multiple lines at once? Here's a tip: In GuidedTrack, simply select all the lines you wish to indent and hit tab to indent them (move them to the right) all together, or hold shift and hit tab to unindent (move them to the left) all together.
Next: Adding blank lines