unfried.
Streak:0 days XP:0

Formula sheet

Thinking in code

51 entries in 8 units. Free to use and share.

Jump to a unit (8 units)

Unit 1 Step by step

How a program runs

Also called sequence, order of instructions.

The computer carries out the instructions one at a time, from the top down, each once.

Careful: It never reorders blocks or fills in a missing one, so the same blocks in another order can do something else.

Taught in Order matters

Move and turn

Also called move forward, turn left, turn right.

move forward goes one square the way the egg faces. turn left and turn right change the facing by a quarter turn and leave the egg on its square.

Careful: A move into a wall or the edge of the grid stops the program at that block.

Taught in A program is a list of steps

Turning rules

Also called facing, three lefts make a right.

Left and right are the egg's own. Four turns the same way bring back the first facing, and three left turns do the same as one right turn.

Careful: A turn left is not toward the left of the screen unless the egg faces up the screen.

Taught in Turning

Say text or say a value

Also called output, quotes.

say "2 + 3" says the characters in the quotes: 2 + 3. say 2 + 3 works out the sum and says 5.

Careful: Working out a sum shows nothing. Only a say block adds to the output.

Taught in Saying things

Algorithm

Also called plan, route plan.

A plan precise enough for a machine to follow with nothing left to guess. Write a route as stretches and turns, then one block per square and per turn.

Applies: Use it before building any program longer than a few blocks.

Taught in Plans before blocks

Unit 2 Remembering values

Assignment

Also called set a variable, store a value.

set b to a works out the right side, then stores that value in the name on the left. It copies once, when the line runs; a keeps its value.

Careful: Assignment never links two names. Changing a afterward does not change b.

Taught in Assignment copies from right to left

Update

Also called set x to x + 1, increment.

set x to x + 1 reads x, works out x + 1 and stores the result in x. It is an instruction, not an equation.

Careful: The right side always uses the value x holds when the line runs, not its first value.

Taught in Updating a value

Set before use

A line can use a name only after an earlier line has stored a value in it. A name with no value is not 0.

Careful: Using a name before it is set stops the program at that line.

Taught in Working things out with variables

Swap with a temporary variable

Save the value that is overwritten first, then overwrite it, then restore it into the other name.

set temp to a
set a to b
set b to temp

Careful: set a to b, then set b to a loses a's value: both end up holding b's.

Taught in Swapping two values

Trace table

Also called state table.

One row per line run and one column per variable. Each row starts from the row above, and each cell holds one value.

Applies: Use it for any program with more than a few changes; it is how tracing is done on paper in an exam.

Taught in State: everything the program remembers

Unit 3 Making decisions

Comparisons at the boundary

Also called more than, at least, less than, at most.

"n is more than 10" is true from 11 up; "n is at least 10" from 10 up. "n is less than 10" is true from 9 down; "n is at most 10" from 10 down.

Careful: "More than" and "less than" leave the boundary value out. Use "at least" or "at most" when it should count.

Taught in True or false

If

The condition is checked once, when the if line runs. If it is true, the indented blocks run; if false, they are skipped.

if temp is more than 20
    say "hot"
say "done"

Careful: A false condition does not stop the program. The next block that is not indented runs either way.

Taught in If

If, otherwise

One condition picks one of two branches. True runs the blocks under the if; false runs the blocks under otherwise.

Careful: The two branches never both run, and one of them always does.

Taught in If, otherwise

Chain of checks

Also called otherwise if, else if.

The checks are tried from the top. The first true one runs its branch, and the rest of the chain is skipped.

if score is at least 90
    say "A"
otherwise, if score is at least 80
    say "B"
otherwise
    say "below B"

Applies: When checks overlap, put the strictest first.

Careful: Separate ifs are each checked and several can run; a chain runs one branch at most.

Taught in Chains of checks

And, or, not

Also called joined conditions, truth table.

"A and B" is true only when both are true. "A or B" is true when at least one is true, both included. "not A" is true when A is false.

Careful: Each part must be a full comparison: "x is equal to 3 or x is equal to 4". And "not (A and B)" means "not A or not B".

Taught in And, or, not

Nested decision

An if inside a branch is checked only when that branch runs. "If A, then inside it, if B" does the same as "if A and B".

Careful: The two match only when neither decision has an otherwise.

Taught in Decisions inside decisions

Unit 4 Repeating

Repeat

Also called counted loop, loop body.

The blocks indented under the repeat (the loop body) run once on each of the 3 passes. The block after the loop runs once, when the loop is done.

repeat 3 times
    say "hop"
say "done"

Careful: Blocks after the loop are not repeated. Only what is indented under the repeat runs again.

Taught in Repeat

Counter

Also called add 1 to, counting loop.

"add 1 to n" is short for "set n to n + 1". A counter changes by one on every pass and keeps its value after the loop.

Careful: After the loop the counter has already changed on the last pass. Start n at 1, say it and add 1 three times, and n ends at 4, not 3.

Taught in Counting with a loop

For k from 1 to n

Also called loop with a counter.

k is 1 on the first pass, then 2, and n on the last, so the body runs n times. Both ends are included.

Careful: The loop changes k by itself, so the body does not add 1 to k.

Taught in Counting with a loop

Running total

Also called accumulator, adding up in a loop.

Set the total to 0 before the loop, add to it in the loop body, and say it once after the loop. This one says 10.

set total to 0
for k from 1 to 4
    add k to total
say total

Careful: A set to 0 inside the loop empties the total on every pass. Without any set to 0, the first add stops the program.

Taught in Running totals

Repeat until, repeat while

Also called condition-controlled loop, while loop.

"repeat until X" ends when X becomes true; "repeat while X" ends when X becomes false. The question is asked at the top of every pass, before the body.

Careful: The question is never asked in the middle of the body, and a loop whose first answer says stop makes no passes.

Taught in Repeat until

Checking the ends of a loop

Also called off-by-one error, fence posts.

Check the first pass and the last pass by hand. The whole numbers from a to b, both included, are b − a + 1 numbers.

Careful: "Less than 5" leaves out the pass for 5; "at most 5" includes it.

Taught in Off by one

Infinite loop

Also called loop that never ends.

A loop ends only when its condition says so. The body must change what the condition checks, in a way sure to reach the stopping point.

Careful: "Equal to" checks can be jumped over: counting down by 3 from 10 never equals 0. "More than 0" can't be jumped over.

Taught in Loops that never end

Nested loop

Also called loop inside a loop.

The inner loop runs all its passes on every pass of the outer loop, so its body runs (outer passes) × (inner passes) times.

Careful: The counts multiply, not add: 3 passes around 4 passes is 12, not 7.

Taught in Loops inside loops

Unit 5 Working through a list

For each item

Also called for loop over a list.

The blocks under the loop run once per item, in order. On each pass n holds the item itself, so this says 40, 70, 20.

set nums to the list 4, 7, 2
for each n in nums
    say n × 10

Careful: n is never the item's place in the list. An empty list runs zero passes.

Taught in For each item

Count and total

Also called counter, running total, accumulator.

A count adds 1 for each item that matches; a total adds the item's value. Both are set before the loop and said after it.

set count to 0
set total to 0
for each n in nums
    add 1 to count
    add n to total
say count
say total

Careful: Set inside the loop, a count or total restarts on every pass and keeps only the last item.

Taught in Count and total

Best so far

Also called maximum, biggest item, largest.

best always holds the biggest item seen so far. For the smallest, replace best when an item is less than it.

set best to the first item of nums
for each n in nums
    if n is more than best
        set best to n
say best

Careful: Starting best at 0 fails when every item is negative. Start from the first item, and only when the list has one.

Taught in The biggest

Search with a flag

Also called found, linear search.

The loop only records a match in the flag. The decision after the loop speaks once.

set found to false
for each n in nums
    if n is equal to target
        set found to true
if found is true
    say "found"
otherwise
    say "not found"

Careful: An otherwise inside the loop speaks after every mismatch, or resets the flag. "Not found" is known only after every item.

Taught in Searching

Stop repeating

Also called break, stop early.

Stop repeating ends the loop at once and the program carries on below it. Put it inside an if so the loop stops only when its job is done.

Careful: The blocks after it on that pass do not run. A search that stops at the first match still checks every item when the target is missing.

Taught in Stopping early

Average of a list

Also called mean.

Total and count together in one loop, then divide once, after the loop.

set total to 0
set count to 0
for each n in nums
    add n to total
    add 1 to count
if count is more than 0
    say total ÷ count
otherwise
    say "no numbers"

Careful: An empty list leaves count at 0, and dividing by 0 fails, so check the count first.

Taught in Putting patterns together: the average

Unit 6 Breaking problems down

Defining and calling a function

Also called define, call.

A define block names its blocks and runs none of them. Each call runs them, then the program carries on after the call.

define turn_right()
    repeat 3 times
        turn left
turn_right()

Careful: A function that is never called never runs. A call made above its define block stops the program.

Taught in Naming a group of steps

Parameters and arguments

Also called inputs.

The parameter (steps) is filled with the argument (3) at each call, so one function works on any value.

define walk(steps)
    repeat steps times
        move forward
walk(3)

Careful: The argument's name need not match the parameter's. The call copies the value, whatever the names.

Taught in Inputs

Give back against say

Also called return value.

Give back ends the call and hands its value to the line that called. A say block only shows a value.

define double(n)
    give back n × 2
say double(5)

Careful: A call alone on its line loses its value, and a function that only says gives nothing back to use.

Taught in Giving back a result

Using a result

A call that gives back a value can go anywhere a value can: a sum, a set block, a condition or another call. The innermost call runs first.

Applies: double(double(3)) works out double(3) first, to 6, then double(6), to 12.

Taught in Using results

Local variables

A function's parameters and the variables it sets exist only during its call. Each call starts with new ones.

Careful: After the call they are gone. Use the value given back, and expect the caller's own variables to be unchanged.

Taught in What stays inside

Decomposition

Split the task into named parts, write a function for each, and build the main program from calls. A repeated part is written once and called once per use.

Applies: Use it when the same steps appear more than once, or when a task is too long to check in one piece.

Taught in Decomposition

Unit 7 Finding bugs

Test cases that catch bugs

Also called edge case, test case.

A test case is an input and the output a right program gives for it. The edge cases to try first: an empty list, one item, all negative numbers, and a value on a boundary.

Careful: One passing test, or several ordinary ones, does not show a program is right for every input.

Taught in Tests that catch bugs

Symptom and cause

Also called trace back, first wrong value.

The symptom is the wrong output you see. The cause is the first line where a value stops matching the plan. Start at the symptom and follow the wrong value back to where it was set.

Careful: The line that says the wrong value is rarely the cause; it shows a value made earlier.

Taught in Symptom and cause

Testing a guess

Also called hypothesis, print debugging.

State one guess about the cause, predict what a say block in one place will show if the guess is right, run it, and compare. Put the say where the guessed problem happens.

Careful: Changing blocks until the output looks right can fix one test and break another. Change one thing at a time.

Taught in Debugging as testing a guess

The usual suspects

Also called common bugs.

BugWhat you seeFix
Blocks in the wrong ordera value said too earlymove the block
Copying the wrong waythe wrong name changesswap the two names
Checks in the wrong ordervalues land in the looser checkstrictest check first
Total reset in the looponly the last item survivesreset before the loop
One pass offfirst or last pass missing or extracheck the first and last pass

Taught in The usual suspects

Regression

Also called re-run every test.

A regression is a fix that breaks a case that used to work. After every change, run every test, old and new.

Careful: A fix is not done when the failing test passes; it is done when every test passes.

Taught in Fixing without breaking

Saying what a program does

Also called summary, misleading name.

Describe the whole effect in one sentence: what the program is given and what it says. Look for a count, a total, a best so far or a flag.

Careful: A variable's name does not decide its value. A variable named biggest holds whatever the blocks store in it.

Taught in What does this program do?

Unit 8 Meet Python

Plain words to Python

Also called block to Python, translation table.

Each plain-word block ran one line of Python. Text in quotes is printed as typed; a name without quotes prints its value.

Plain wordsPython
say "hi"print("hi")
set score to 0score = 0
add 1 to nn += 1
say double(5)print(double(5))

Taught in The same program in Python

Assignment and comparison

Also called = and ==, equals sign.

A single = stores the right side in the name on the left. == compares and stores nothing.

n = 10       # set n to 10
n == 10      # is n equal to 10?
n != 10      # is n not equal to 10?

Careful: A single = inside an if or while check is a syntax error, and Python runs no line of the program.

Taught in Decisions in Python

Comparisons in Python

Plain wordsPython
is more than, is at least>, >=
is less than, is at most<, <=
is equal to, is not equal to==, !=
n is evenn % 2 == 0

Taught in Decisions in Python

if, elif, else

Also called decisions in Python, indentation.

Each check ends with a colon. The lines indented under it are its branch; the first line back at the left edge runs either way.

if score >= 90:
    print("A")
elif score >= 80:
    print("B")
else:
    print("below B")
print("done")

Careful: Indentation is part of the program, not decoration: pushing a line in moves it inside the branch.

Taught in Decisions in Python

range in a for loop

Also called for loop, counted loop.

range(a, b) starts at a and stops before b. With one number, range(n) gives 0 to n − 1: n passes.

for k in range(1, 6):  # 1 to 5
    print(k)
for _ in range(3):     # 3 passes
    print("hop")

Careful: To include b, write range(a, b + 1). "for k from 1 to 5" is range(1, 6).

Taught in Loops in Python

while loop

Also called condition loop, repeat until.

The check runs at the top of each pass, and the whole body runs once a pass has started. "Repeat until X" is while not X:.

while n > 0:
    print(n)
    n = n - 1

Careful: Python has no until loop. The loop stops only when the check at its top is false, never halfway through the body.

Taught in Loops in Python

Lists in Python

Square brackets make a list; for n in nums: takes each item in turn; len is the length.

nums = [3, 1, 4]
for n in nums:
    print(n)
print(len(nums))  # 3
print(nums[0])    # first item

Careful: Positions count from 0, so the first item is nums[0]. The Python course teaches positions.

Taught in Lists and functions in Python

def and return

Also called function in Python, give back.

def defines a function (define), return gives a value back to the caller (give back), and the call runs it.

def double(n):
    return n * 2

print(double(5))   # 10

Careful: print inside a function shows a value but gives back None. Use return when the caller needs the result.

Taught in Lists and functions in Python