The debugging loop
Good debugging follows a repeatable cycle:
- **Reproduce*: make the bug happen on purpose.
- **Hypothesize*: guess the most likely cause.
- **Test*: change one thing and see if it fixes it.
- **Confirm*: verify the fix and that nothing else broke.
Reproduce -> Guess -> Change one thing -> Check -> Repeat
Change only one thing at a time, or you won't know what fixed it.
Common bug types
Most beginner bugs are a small set:
- **Off-by-one*: looping one time too many or too few.
- **Wrong operator*:
=instead of==. - **Uninitialized variable*: using a value before setting it.
- **Index out of range*: asking for item 5 in a 5-item list.
# off-by-one: range(1, 5) gives 1,2,3,4 (not 5)
for i in range(1, 5):
print(i)
Spotting which category a bug belongs to makes the fix fast.