Values, names, decisions, and loops
Learn the programming ideas needed to build a recogniser before a model is involved.
Start with values
The next lesson puts a trained model inside a program. Before that, this lesson teaches the handful of programming ideas the program is made of: values, names, decisions, loops, lists, and typed input. Each stage below is a complete small program. Try this stage loads it into the editor beside the article. Predict the output, then press Run.
A value is a piece of information. 12 is a number you can add. hello is text you can print. True and False are the two possible answers to a yes-or-no question. A list holds several values in order, such as the moves rock, paper, scissors.
Block shapes show what fits where. A rounded value block, such as a variable or a comparison, supplies a value. A statement block, such as say, does something with it. A value block never prints itself; the statement around it decides what happens to its value.
Names remember values
The first two stages store a number under a name and then change it.
Build with me · 1
Give a number a name
A variable is a name for a value during a run. The first statement stores the number 2 under score. The second statement reads it. Storing and displaying are different operations; the set block can work correctly without printing anything.
The rounded score value belongs inside the output socket. The word Score in the label field is ordinary text for the reader. Those two similar-looking words have different jobs.
Add set from Basics, name it score, and enter 2. Under it add a labelled say, type Score as the label, and insert the rounded score variable into the value socket.
What to look for
The output is Score 2.
Make it yours
Choose another small number. Predict the single output line before running.
Build with me · 2
Replace a value by adding to it
Change score by 3 reads the old score, adds three, and stores the result back under the same name. It does not create a second score. The computer follows the stack from top to bottom, so the final output sees five.
This is also how a game score works: a running value is updated when something happens. If you put the first set block after the change, it would overwrite the result with two.
Insert change score by 3 between the assignment and output. Check the insertion line before releasing the dragged block. Keep the set block above the change.
What to look for
The output is Score 5.
Make it yours
Move the output above the change, predict what it will show, then restore it. The arithmetic did not change; the observation happened earlier.
The name score referred to two at first and five later. Storing a new value replaces what the name refers to; it is not an equation that stays true forever.
Names are case-sensitive in the Python underneath, so Score and score are different names. Choose an existing name from a block's menu when it offers one. If a name is missing from the menu, the block that creates it is usually absent or further down the stack.
Ask a question, then choose a path
A program often needs to ask a yes-or-no question about a value and then act on the answer.
Build with me · 3
Ask a true-or-false question
A comparison answers a yes-or-no question with one of two values: True or False. Programmers call a true-or-false value a Boolean. Here the operator >= means greater than or equal to, so exactly five counts as True. The comparison is a value block, so it needs an enclosing instruction to do something visible with its result.
The comparison reads the variable; it does not change it. In Python the same question will be score >= 5. You are already choosing the calculation before learning its typed punctuation.
Put a comparison in the labelled output socket. Put score on its left, choose >= in its menu, and type 5 on its right.
What to look for
With score five, the output is Ready? True.
Make it yours
Try scores four, five, and six. Then use > instead of >= and explain exactly which case changes.
Build with me · 4
Choose one of two branches
An if/else uses that True or False answer to decide which body to run. With score five it enters the first body; with four it enters otherwise. After that body finishes, execution continues below the whole if/else.
Indentation in the illustration shows ownership. A say placed below the closing edge would run regardless of the comparison. A say placed inside otherwise runs only when the comparison is false.
Move the comparison into the if/else test socket. Put Ready inside the first opening and Keep practising inside otherwise.
What to look for
Exactly one verdict prints: Ready for five.
Make it yours
Choose your own threshold and two messages. Test below the threshold, at it, and above it.
The comparison menu also offers is, which asks whether two values are equal. In Python, asking about equality is written ==, and storing a value is written =. A condition asks about values; it never changes them.
Sometimes one question is not enough. Rock, paper, scissors needs two facts at once: "my move is rock and their move is scissors" is a win. Checking only that my move is rock would also award a win against paper. The and block combines two conditions; its partner or is true when at least one of its conditions is true.
Build with me · 5
Require two conditions together
AND is true only when both comparisons are true. A high score alone is not enough, and a ticket alone is not enough. Build each comparison first, then put those two completed value blocks inside the logic block.
The word yes here is an ordinary text value; the comparison converts the question about that text into True or False. Equality checks whether the stored text matches. It does not set has_ticket to yes.
Nest score >= 5 and has_ticket is yes in the two AND sockets, then put AND in the if test.
What to look for
The supplied values print You may enter.
Make it yours
Test all four combinations of enough/not-enough score and yes/no ticket. Change AND to OR and identify why more combinations now enter.
Repeat work with a loop
A repeat block runs the blocks inside it a fixed number of times. Where you place a block, inside the loop or below it, decides how often it runs. When a loop gives a surprising result, trace it one turn at a time with your finger before changing anything.
Build with me · 6
Trace a loop one turn at a time
A running total needs an initial value before the loop. Each turn adds two to the value left by the previous turn. Follow the values: zero before the loop, two after the first turn, four after the second, six after the third.
Now is inside the loop and prints every turn. Finished is below the loop and prints once. Repeated output is a useful trace while you are learning or debugging.
Create total before repeat. Place change and Now inside repeat, and Finished below its closing edge.
What to look for
The lines show Now 2, Now 4, Now 6, then Finished 6.
Make it yours
Move set total to 0 inside repeat. Predict the repeated value, run, then Undo the move and run again.
Lists, and a loop for each item
The game will need its three moves stored together, in a fixed order. A list does that, and a for-each loop visits each item in turn.
Build with me · 7
Keep several related values
A list keeps values in a particular order. Creating an empty list and appending three items gives rock, then paper, then scissors. The name moves refers to the whole collection, not one particular move.
The item block uses friendly positions beginning at one. Python lists will begin at zero; the generated code translates the block position. The length is a count, so three items still means a length of three in either language.
Create moves from Lists. Connect three add blocks in order, then place item 1 of moves inside the output socket.
What to look for
First move is rock and Count is 3.
Make it yours
Swap the paper and scissors add statements. Which output changes? Try item 2 to inspect the changed order.
Build with me · 8
Visit every item with a name
For each creates the name move for the current item. On the first turn move is rock; on the next it is paper; on the last it is scissors. The loop handles the number of turns based on the list.
Use the rounded move variable inside the body. Typing move as text would print the same word three times. The final message sits outside the body so it is a single completion message.
Add a for-each block, select moves, and name the loop item move. Nest a labelled output reading move inside it.
What to look for
Three Choice lines print in list order, followed by one completion message.
Make it yours
Add a fourth harmless text item and run again. Notice that you did not need to change a repeat count.
Input and a complete small program
So far every value was typed into a block before the program ran. A program can also ask the person using it. the line typed in waits for an answer in Terminal and supplies it as text. the number typed in also waits, then turns the answer into a number so you can do arithmetic with it. Text 12 and the number 12 look the same on screen, but only the number can be added to.
Build with me · 9
Read text and numbers in Terminal
When this stack reaches its text question, Terminal pauses and waits for an answer. Type a fictional name, then press Enter or Send. It then waits again for the number block; type a numeral for the age and send it. Each answer reaches one asking block in order, and the number answer becomes usable in arithmetic.
An empty or nonnumeric age cannot be converted into a number; that is an input problem, not evidence that addition is unreliable.
Press Run. When Terminal first waits, answer with a fictional name. When it waits again, answer with a number. Watch the stack resume after each answer.
What to look for
After answers Ada and 12, output greets Ada and gives a next birthday of 13.0; the decimal is normal because the number block reads every answer as a decimal number.
Make it yours
Run again with a different fictional age. Then deliberately enter twelve for the numeric answer, read the conversion error, and rerun with a numeral to repair it.
The last stage uses every idea from this lesson in one short program.
Build with me · 10
Combine input, arithmetic, a decision, and repetition
This tiny fictional story launcher uses the same four ideas together. It reads two Terminal answers, selects a story branch, then counts down. The age rule belongs to this invented story; it is not an account-access or safety rule.
Read the complete stack in sections before running. The first Terminal answer determines the greeting and the second determines the story branch. Countdown starts at three and is independent of age. Keeping those jobs under distinct names makes the flow easier to explain and debug.
Build the two input assignments, the branch, and the countdown in order. Keep the initial countdown outside repeat. Press Run, answer when Terminal waits twice, then personalise the answers on another run.
What to look for
After answers Ada and 12, the program greets Ada, chooses the younger-character story, prints 3, 2, 1, and prints Start once.
Make it yours
Change the fictional story threshold and messages. Keep the countdown at three while testing the branch so only one behaviour changes at a time.
Before adding a model, make sure you can point at a program and say where it gets a value, which name stores it, which condition uses it, and which statement prints the result. In the next lesson a model becomes one more step in exactly this kind of program.
Full reference solution
This is the complete worked program. Try building it yourself first, then use this reference to find the first place your version behaves differently. The Python below is generated from these exact blocks; helper functions are included so its behaviour can be inspected.
name = input()age = float(input())print("Hello", name)if (age >= 13): print("Choose the older-character story")else: print("Choose the younger-character story")countdown = 3for _ in range(int(3)): print("Starting in", countdown) countdown = countdown + -1print("Start")Compare this with your version. Different names and personal choices are fine when the program follows the same logic.
Keep your progress
Sign in and every reading, quiz, and exercise you finish is saved.