Understanding that mistakes (bugs) happen and learning how to find and fix them.
Imagine you told a robot to make a peanut butter sandwich, but instead of putting the jelly on the bread, it smeared it on its own head! Did the robot lose its mind, or was there just a 'bug' in the plan?
Robots are very good at following directions, but they aren't very good at 'guessing' what you mean. They do exactly what you tell them to do. If you give a robot the wrong instruction, it will make a mistake. In computer science, we call a mistake in a set of instructions a bug.
Think of a set of instructions like a recipe. If the recipe says to add cups of salt instead of grains of salt, the cookies will taste terrible! The cookies aren't 'broken,' the instructions have a bug. Finding and fixing these mistakes is a super-power called debugging.
Let's look at a sequence for putting on socks and shoes. Can you find the mistake?
1. Pick up your socks. 2. Put on your shiny shoes. 3. Put on your socks. 4. Tie your laces.
In this -step sequence, Step and Step are in the wrong order! You can't put socks on over your shoes. To debug this, we just swap them.
Quick Check
In your own words, what is a 'bug' in computer science?
Answer
A bug is a mistake in a set of instructions that causes a program or robot to do something unexpected.
To find a bug, computer scientists use a technique called tracing. This means you follow the instructions one by one, very slowly, acting out exactly what each step says.
Imagine a robot on a grid at position . If the goal is to get to position , and the instructions are: - Step 1: Move - Step 2: Move - Step 3: Move - Step 4: Move
By tracing, we see the robot goes: . It never reaches ! The bug is in Step because it told the robot to go backward instead of forward.
A robot needs to draw a square. To draw a square, it needs to move and turn times.
1. Move Forward 2. Turn Right 3. Move Forward 4. Move Forward
If we trace this, the robot moves, turns, moves, and then moves again in a straight line. It forgot to Turn Right again! To fix it, Step should be 'Turn Right'.
Quick Check
What is the name of the technique where you follow instructions one-by-one to find a mistake?
Answer
Tracing
Once you find a bug, you have to fix it! This is the most important part of 'Thinking Like a Robot.' Even the best coders in the world make bugs every single day. They don't get upset; they just get out their 'magnifying glass' and start debugging.
When you debug, you usually do one of three things: 1. Remove an extra step. 2. Change a wrong step to a right one. 3. Add a missing step.
Remember: !
Help the robot get to the gold star! The robot is facing North. The path to the star is: 'Forward, Forward, Right, Forward'.
Here is the buggy code: 1. Move Forward 2. Turn Left 3. Move Forward 4. Move Forward
The Debugging Steps: 1. Step 1 is correct (Forward). 2. Step 2 is a bug! It should be 'Turn Right' instead of 'Turn Left'. 3. Step 3 is correct (Forward). 4. Step 4 is an extra step! The robot only needs to move forward once after turning to reach the star. We should remove it or change it.
What do we call a mistake in a computer program?
If a robot is supposed to walk steps but only walks , what should you do?
Tracing means acting out instructions one-by-one to find a bug.
Review Tomorrow
Tomorrow morning, try to 'trace' your own routine for brushing your teeth. Did you forget to put water on the brush? That's a bug!
Practice Activity
Draw a simple grid. Place a 'toy' at the start and a 'goal' at the end. Write instructions to get there, but purposely put a 'bug' in one step. Ask a friend to 'debug' your code!