Year 7 · Spring 2

Scratch I: Programming essentials · Lesson 5 of 5

Debugging with PRIMM

PracticalTopic 11 Algorithms

Year 7 · Scratch I: Programming essentials · Lesson 5 of 5

You will learn to

  • Use PRIMM to read a script: predict what it does before running it.
  • Identify the block where a buggy script first goes wrong.
  • Fix a bug, test it again, and explain the fix in one sentence.

Year 7 · Scratch I: Programming essentials · Lesson 5 of 5

Success criteria

You have got it when you can say:

  • I can write my prediction down before I click the green flag.
  • I can find and fix the bug in three buggy scripts.
  • I can explain each fix in one clear sentence.

Year 7 · Scratch I: Programming essentials · Lesson 5 of 5

Retrieval starter

Questions from before. Write your answers, then show them.

  1. Last lesson

    In a flowchart, a decision is a…

    A DiamondB OvalC Rectangle

    A. Diamond

  2. Last unit

    Every spreadsheet formula starts with…

    A #B =C $

    B. =

  3. Earlier this year

    A fake bank email asking you to log in is…

    A PhishingB Brute forceC A worm

    A. Phishing

Year 7 · Scratch I: Programming essentials · Lesson 5 of 5

Key words (1 of 2)

Algorithm
Precise, ordered, finite steps to solve a problem.
Decomposition
Breaking a problem into smaller ones.
Abstraction
Ignoring detail that doesn't matter for the problem.
Sequence · selection · iteration
Steps one after another. First this, then this.

Year 7 · Scratch I: Programming essentials · Lesson 5 of 5

Key words (2 of 2)

Pseudocode
Code-like writing for algorithms, without strict rules.
Trace table
Records variable values step by step to check logic.
Variable
A named place in memory that holds a value.

The explanation · step 1 of 4

Bugs are normal

A bug is a mistake in a program that makes it do the wrong thing.

Debugging means finding bugs and fixing them.

Every programmer writes bugs. Good programmers find them calmly, one step at a time.

★Remember

Scratch blocks only snap together where they fit. So most Scratch bugs are logic errors: the script runs, but does the wrong thing.

The explanation · step 2 of 4 · part 1 of 2

PRIMM: a way to read code

PRIMM is five steps for working with a program someone else wrote.

LetterStepWhat you do
PPredictRead the code. Say what you think it will do, before you run it.
RRunRun it and watch. Was your prediction right?
IInvestigateLook closely. Trace it block by block. Change one thing and see what happens.
MModifyChange the code so it does something new.
MMakeWrite your own program using the same idea.

The explanation · step 2 of 4 · part 2 of 2

PRIMM: a way to read code (continued)

?Why?

Why predict first?

A wrong prediction is useful. The place where the code surprises you is where to look for the bug.

The explanation · step 3 of 4 · part 1 of 2

Debugging step by step

  1. Predict: what should the script do?
  2. Run it.
  3. Compare: what did it really do? Where is it different?
  4. Find the first block where it goes wrong.
  5. Fix one thing at a time.
  6. Run it again to check the fix works.
  7. Explain the fix in one sentence.

The explanation · step 3 of 4 · part 2 of 2

Debugging step by step (continued)

!Watch out

Change only one thing, then test. Change three things at once and you will not know which one fixed it.

The explanation · step 4 of 4

Common Scratch bugs

BugWhat you seeFix
Variable never resetThe score starts at the last game's numberPut set [score v] to (0) under when green flag clicked
Wrong signLives go up instead of downUse change [lives v] by (-1)
Missing loopThe sprite moves once, then stopsPut the blocks inside forever or repeat
Wrong numberRepeat 3 with turn 90 draws only three sides of a squareUse repeat (4)
Blocks in the wrong orderThe sprite says 'Done!' before it movesDrag the say block below the move blocks
Script on the wrong spriteThe wrong sprite movesDrag the script onto the right sprite

Year 7 · Scratch I: Programming essentials · Lesson 5 of 5

Worked example

This script should count lives down from 3 and then say 'Game over'. It never says it. Why? Script: set lives to 3. Repeat until lives = 0: wait 1 second, change lives by 1. Say Game over.

  1. Predict: lives should go 3, 2, 1, 0, then the sprite says 'Game over'.
  2. Run: lives go 4, 5, 6, 7 … and never stop.
  3. Compare: lives go up, not down, so they never reach 0.
  4. Find: the block change [lives v] by (1) adds 1.
  5. Fix: change it to change [lives v] by (-1) and run it again.
  6. Explain: 'The loop never ended because lives went up, so they never reached 0.'

Year 7 · Scratch I: Programming essentials · Lesson 5 of 5

Quick quiz · 1 of 3

How many times does FOR i ← 2 TO 8 repeat?

  1. A 8
  2. B 6
  3. C 7

C. 7

Both ends count: 8 − 2 + 1 = 7 (2, 3, 4, 5, 6, 7, 8).

Year 7 · Scratch I: Programming essentials · Lesson 5 of 5

Quick quiz · 2 of 3

Which loop always runs at least once?

  1. A FOR i ← 1 TO 0
  2. B REPEAT … UNTIL
  3. C WHILE … ENDWHILE

B. REPEAT … UNTIL

REPEAT … UNTIL checks its condition at the end, after the first run.

Year 7 · Scratch I: Programming essentials · Lesson 5 of 5

Quick quiz · 3 of 3

x ← 4, then x ← x × 2, then x ← x + 1. What is x?

  1. A 8
  2. B 12
  3. C 9

C. 9

4 × 2 = 8, then 8 + 1 = 9. Each line replaces the old value.

Year 7 · Scratch I: Programming essentials · Lesson 5 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. Which shape is used for OUTPUT total?

    A ParallelogramB DiamondC Rectangle

    A. Parallelogram Input and output are always parallelograms.

  2. Breaking a problem into smaller parts is…

    A IterationB DecompositionC Abstraction

    B. Decomposition De-compose = take apart.

Year 7 · Scratch I: Programming essentials · Lesson 5 of 5

Homework and the challenge

Homework

Study page 11 to step 5; the 'Trace a loop' practice.

End-of-unit challenge

Trace and fix three buggy scripts, then extend one with a new feature.