Debugging, explained simply
A bug is a mistake in a program. Every programmer, including professionals, writes bugs every day. What makes someone good at coding is how calmly they find and fix them.
Start with the error message. The last line names the kind of problem and the line number shows roughly where it is. Common ones for beginners are NameError (a misspelt name), SyntaxError (a missing colon or bracket) and IndentationError.
Some bugs do not cause an error at all: the program runs but does the wrong thing. For those, add print() lines to check what your variables hold at each step, and change one thing at a time.
Example: Spot the two bugs
steps = 3
for step in range(steps)
print("Step", stpe)Output
SyntaxError: expected ':'Add the colon after range(steps), run it again, and Python will point you to the second bug: stpe should be step.
Try this
- Fix the example one bug at a time and read each new error message.
- Break a working program on purpose in three different ways and write down each error name.
- Explain your code out loud to a toy or a family member. It is a real technique called rubber duck debugging.
