Learning goals
- Run code step by step with a debugger.
- Check which line runs and what the turtle draws.
- Find a bug by checking your code carefully.
- Explain how debugging helps you understand code.
A bug is a mistake in your code. Looking for bugs and fixing them is called debugging.
The computer always does exactly what you wrote. So if the result is wrong, the bug is in the code.
Some bugs give an error. You know these already, such as a capital letter or a missing parenthesis. Other bugs give no error. The program runs, but the turtle draws something else.
This program should draw a square. What does the turtle draw?
import turtle as otto
otto.forward(80)
otto.left(90)
otto.forward(80)
otto.left(90)
otto.forward(80)
otto.left(90)

otto.forward(80). Add otto.forward(80) at the end.An error message tells you on which line Python stopped. For example: line 4.
The code editor shows a number in front of each line. Go to the line with that number and look for the bug there.
Sometimes the bug is on the line just before it. For example, Python often notices a missing ) only on the next line.
Finding a bug is like looking for Waldo in a big picture. Look at each line, one by one.
Check these common mistakes:
otto.Forward() instead of otto.forward().( or ) missing?"red".otto the same way everywhere?Two more tips:
A debugger lets you run your code one step at a time and see what happens behind the scenes. This helps you understand your code. You can see which line just ran and what the current state is. This helps you understand what your code really does. You can also find out why your drawing looks different from what you expected.

When you start the debugger, you will see controls for stepping through your code:

| Idea | What it means |
|---|---|
| Bug | A mistake in your code. |
| Debugging | Looking for bugs and fixing them. |
| Error | Python stops and shows a message. |
| Wrong drawing | The code runs, but the turtle draws something else. |
| Debugger | A tool that runs your code one step at a time. |