Make decisions
You can make your program take different steps depending on what's true. You write a check, like age is at least 18, and Plain takes the steps under it only when the answer to the check is yes.
Take steps only when a check is yes
Start the line with if, then write the check. Put the steps that depend on it on the lines below, moved 4 spaces to the right. The Tab key does this for you.
ask "How old are you?" and call it age
if age is at least 18
show "You can vote."
show "Thanks for asking."Try itWhat you type:
41
What it shows:
How old are you? 41
You can vote.
Thanks for asking.
If you answer 15, Plain skips show "You can vote.". The last line is back at the left edge, so it isn't part of the if. Plain always shows it.
Choose between two sets of steps
Add otherwise to say what to do when the check is no:
ask "How old are you?" and call it age
if age is at least 18
show "You can vote."
otherwise
show "You can vote in {18 minus age} years."Try itWhat you type:
15
What it shows:
How old are you? 15
You can vote in 3 years.
Check more than two things
Use otherwise if for each extra check. Plain tries the checks from the top and takes the steps under the first one that's yes. It skips all the others. otherwise at the end covers everything else:
ask "What's the temperature outside?" and call it temperature
if temperature is more than 25
show "It's hot. Take water."
otherwise if temperature is less than 5
show "It's cold. Take a coat."
otherwise
show "It's a nice day."
show "Have a good walk."Try itWhat you type:
2
What it shows:
What's the temperature outside? 2
It's cold. Take a coat.
Have a good walk.
Compare values in words
You can write these checks:
isandis not: the same, or not the same.is more thanandis less than.is at leastandis at most: these count the number itself, so 18 is at least 18.is between 1 and 10: both 1 and 10 count.contains,starts withandends with: for text.is empty,is a number,is a whole number,is evenandis odd.
If you're used to =, > and <, those work too.
ask "Pick a number from 1 to 10." and call it pick
if pick is between 1 and 10
show "Thanks!"
otherwise
show "{pick} is not from 1 to 10."Try itWhat you type:
12
What it shows:
Pick a number from 1 to 10. 12
12 is not from 1 to 10.
When Plain compares text, it ignores capitals and any spaces at the start or end. So "Pineapple" counts as "pineapple":
ask "What would you like on your pizza?" and call it toppings
if toppings contains "pineapple"
show "Bold choice!"
otherwise
show "Coming right up."Try itWhat you type:
Ham and Pineapple
What it shows:
What would you like on your pizza? Ham and Pineapple
Bold choice!
When capitals must match too, write is exactly.
Check two things at once
Join two checks with and when both must be yes. Use or when either one is enough:
ask "How old are you?" and call it age
ask "Do you have a ticket?" and call it ticket
if age is at least 18 and ticket is yes
show "Come on in."
otherwise
show "Sorry, not tonight."Try itWhat you type:
30
y
What it shows:
How old are you? 30
Do you have a ticket? y
Come on in.
If you use both and and or in one check, put brackets around the part that goes together. Otherwise Plain asks you to, because it never guesses which one you meant.
Leave out the words you'd repeat
You don't have to write answer is again after or. Plain uses the start of the check again for you:
ask "Do you want to hear a joke?" and call it answer
if answer is "y" or "yes" or "sure"
show "Why did the bicycle fall over? It was two tired."
otherwise
show "Maybe later."Try itWhat you type:
Sure
What it shows:
Do you want to hear a joke? Sure
Why did the bicycle fall over? It was two tired.
This works when the next part is a value, like "yes" or 100, or starts with a comparison, like more than 100. So guess is less than 1 or more than 100 checks guess both times.
Plain stops you when the short form can't be right:
ask "Do you want to hear a joke?" and call it answer
if answer is "y" and "yes"
show "Here goes."Try itWhat Plain says:
your program, line 2: Use "or" here, or write the whole check again after "and".
if answer is "y" and "yes"
^^^^^
Why: A value can't be two things at once. Did you mean "or"?
Plain found 1 thing to fix, so it hasn't run your program yet.
After is not and is between, write the whole check each time. A short form there could mean two different things, so Plain asks you to write it in full.
Use yes and no
When you compare an answer with yes or no, Plain counts "yes", "y" and "true" as yes, and "no", "n" and "false" as no. Capitals don't matter:
ask "Is it raining?" and call it raining
if raining is yes
show "Take an umbrella."
otherwise if raining is no
show "Leave the umbrella at home."
otherwise
show "Please answer yes or no."Try itWhat you type:
Y
What it shows:
Is it raining? Y
Take an umbrella.
You can also keep yes or no under a name, as in set finished to no, and check it later with if finished is yes.
Write a short decision on one line
When there's only one step, you can put it on the same line, after then:
set score to 10
if score is 10 then show "You win!"Try itWhat it shows:
You win!
The one-line form takes just one step and no otherwise. For anything more, put the steps on their own lines.
Try it yourself
Press Try it on any example above, then change it:
- Change the voting age in the first two examples to 16.
- Ask for the temperature and show "It's freezing!" when it's at most 0.
- Ask "Are you hungry?" and suggest a snack only when the answer is yes.
- Ask for a word, and say whether it starts with "a".