Introduction
15 minLast lesson ended with two experiments and no answers. Here they are.
You typed wall with no number. Nothing happened. No wall, no message, no error — the program ran all the way through and built nothing at all. That is because a chat number you do not supply arrives as 0, and repeat 0 times is a perfectly good instruction to do something no times.
You typed wall 40. The Agent set off confidently and stopped three-quarters of the way up, out of stone, leaving a broken thing behind it.
Two different disasters, one cause: your program does what it is told without ever asking whether it makes sense.
The first block that can skip something
Every block you have written so far runs. Every time, in order, no exceptions. Loops run things more than once, but nothing has ever been skipped.
if changes that.
if
something is truethen … do these blocks. Otherwise, do not.
And its partner:
else … do these other blocks instead.
Exactly one of the two happens. Never both, never neither.
A condition is a question with a yes or no answer
The Logic drawer holds a comparison block — two slots with a dropdown between them:
| Means | |
|---|---|
= | is the same as |
≠ | is not the same as |
< | is less than |
> | is more than |
≤ | is less than or the same as |
≥ | is more than or the same as |
Drop a variable in one side and a number in the other and you have a question the program can answer: is size more than 8? Yes or no. Nothing in between.
Two jobs for one block
if does two quite different things in this lesson, and it is worth knowing which you are doing:
- Guarding — refusing to act on a number that cannot work. If the number is silly, say so and build nothing.
- Choosing — acting differently depending on the number. If the build is big, use glass; otherwise use stone.
Guarding protects the program from the player. Choosing makes the program cleverer than the person who wrote it.
Warm-up — rules you already follow
You have obeyed conditions all your life. If the light is red, stop — else go. If it is cold, take a coat.
Notice that every one of them splits the world in two, and you always end up on exactly one side. That is all an if block is.
What you will be able to do
- Ask a yes-or-no question about a number.
- Refuse a number that cannot work, and say why.
- Make one program build two different things depending on what it is given.
- Write a program that cannot be broken by whatever the player types.
Basic
20 minFollow along on your own computer. Everyone should finish this part with the same working program.
Step 1 — Watch it fail, on purpose
Open your wall program from Lesson 5 and go into the world.
- Type
wallwith no number. Nothing happens at all. - Type
wall 40. Watch the Agent stop partway and leave a broken wall.
Neither of those is the program's fault, exactly. Nobody ever told it what a sensible number looks like.
Step 2 — Find the Logic blocks
- Press
Cand open your project. - Click the Logic drawer — it is dark blue-green.
- Drag out an if … then block, and a comparison block (the one that looks like
0 = 0).
Click the dropdown in the middle of the comparison block to see the six choices. Set it to >.
Step 3 — Guard the top end
Put size in the left slot and 8 in the right, so it reads if size > 8 then.
Now click the (+) at the bottom of the if block. It grows an else. Click it again and you get an else if as well.
Arrange it like this — the whole build moves inside the final else:
Challenge 1
15 minA tower that argues back.
Your tower command from Lesson 5 will accept any number at all, including ones that cannot work.
- Press
Cand open yourtowerprogram. - Add a guard so that a height of less than 1 makes the Agent say something helpful and build nothing.
- Add a second guard so that a height of more than 30 also refuses, with a different message saying what the limit is.
- Any height in between builds the tower exactly as it did before.
- Go into the world and try:
tower,tower 0,tower 50,tower 12.
Think about which test has to come first. Step 5 of Basic explains why that is not a free choice.
It works if: the first three commands each produce a message and no blocks at all, the two messages are different from each other, and tower 12 still builds a tower twelve blocks tall.
Challenge 2
20 minA room that picks its own material.
Take your room command from Lesson 5 and let it choose what to build with.
- Press
Cand open yourroomprogram. - Guard it first: anything less than 2 or more than 8 gets a message and no build.
- Then make the room choose its material: rooms with a side of 5 or more are built in glass, and smaller ones in stone.
- The choice must happen inside the program. You may not type the material in yourself between runs.
- Try
room 3, thenroom 6, thenroom 20.
It works if: room 3 gives a small stone room, room 6 gives a bigger glass one, room 20 gives a message and no blocks, and you never touched the program in between.
Challenge 3
25 minThe program that cannot be broken.
Every guard so far has refused. Refusing is safe, but it is not very helpful — the player still gets nothing. A better program fixes the number and builds anyway.
- Press
Cand start a new chat command calledsafe. - Give it one number and rename it
size. - Make it build a square outline — but whatever the player types, it must always build something sensible:
- a number that is too small should be pulled up to the smallest one that works,
- a number that is too big should be pulled down to the biggest one that fits in a slot,
- anything in between should be used exactly as typed.
- The
setblock from the Variables drawer is what changes a number the player gave you. A guard can do more than complain. - Go into the world and type
safe,safe 1,safe 4,safe 500andsafe 7.
It works if: all five commands build a square you can see — none of them do nothing, and none of them leave a half-finished shape — and the ones you typed sensibly come out exactly the size you asked for.
Extra challenge
spare timeLet the program do the arithmetic.
Since Lesson 4 you have been multiplying by hand before every build, to check it fits in a slot of 64. Make the program do it.
- Press
Cand start a new chat command calledhut. - Give it two numbers, renamed
sideandheight. - A hollow hut is four walls,
sidelong,heighthigh — so work out the expression that gives the number of blocks it needs. The Math drawer has a multiply block, and one multiply block can sit inside another. - If that number is more than 64, the Agent should say how many blocks were asked for and build nothing.
- Otherwise, build the hut.
- Try
hut 4 3, thenhut 8 8, thenhut 5 3.
It works if: hut 8 8 refuses and tells you the number it worked out, hut 4 3 and hut 5 3 both build, and you did not write the number 64 anywhere except in the comparison.
Why this is the best program in the section so far
Every other guard you have written today compares one number the player typed against one number you decided on.
This one works something out first, and then decides. The program is no longer checking the input — it is checking a consequence of the input, something nobody typed and nobody knew until it was calculated.
That is the difference between a program that follows rules and a program that reasons. Keep this command; you will want it every time you build for the rest of the section.