GoalFind a mistake in a program by comparing it with the algorithm, fix it and extend the program.
New words
bug · a mistake in a programdebug · to find and fix the bugs in a programextend · to add a new feature to a programsense · one of the five ways in which people take in information
Explanation
A bug is a mistake in a program. To debug, compare the program with the algorithm one block at a time. Common bugs are the wrong button, no pause (so the picture flashes by too fast), a missing clear screen, a wrong number in a repeat block and blocks in the wrong order. Fix one bug at a time and test again in the simulator. To extend a program is to add something new, such as a second button. People have input devices too: eyes for sight, ears for hearing, skin for touch, a nose for smell and a tongue for taste. The brain is the processor.
Bug
What you notice
The fix
wrong button
nothing happens when you press A
change B to A
no pause
the picture flashes by too fast to see
add a pause block
no clear screen
the old picture stays
add clear screen
wrong repeat number
the picture shows too few times
change the number
Worked examples
The algorithm says: when button A is pressed, show the heart for 2 seconds and clear the screen, 3 times in all. The red block is the bug. The pause is only 1 second, so it must be changed to 2 seconds.
Extending a program. The first script was already working. The second script is new: it makes button B show the duck icon 2 times.
eyes see a red light→brain decides→legs stop
A person works like a board. The eyes are a sensor, the brain is the process and the legs are the output.
Class activity
Debug detectives (in pairs): each person writes a small algorithm and the matching program on paper, with blocks on separate cards. Hide one bug, for example a wrong button, a missing pause or a repeat number that does not match. Swap. The detective traces the program step by step with a finger and finds the bug. Then fix it and say which type of bug it was. Finish by naming one thing that each of your five senses noticed today.
Practice
1
The algorithm says the house is shown for 3 seconds after button A is pressed. In the program the house never seems to appear. What is the bug?
There is no pause block between show icon and clear screen. The house is cleared at once. Add pause 3 seconds.
2
The algorithm says to show the diamond 4 times in all. Find the bug and fix it.
The repeat block says 3. It must say repeat (4) times.
3
Which sense is the input for each of these? A smelling smoke, B hearing a bell, C feeling a hot cup.
A the nose (smell), B the ears (hearing), C the skin (touch).
4
Extend the program in question 2 so that it also shows the diamond 4 times when button B is pressed. What do you add?
Add a second script that starts with on button B pressed and has the same repeat (4) times block with show icon diamond, pause and clear screen.