MathubLearn / Practice / Excel
Mathub study guide · 5 min read · programming · debugging

Getting unstuck on a coding problem

Errors are not failures; they are the computer telling you exactly where to look. A repeatable routine for CSCI 127.

Everyone who codes gets stuck constantly; the difference between beginners and experienced programmers is what they do in the next five minutes. This is the routine.

Read the error, all of it

Make it smaller

  1. Print the values just before the failing line: `print(i, len(items))`. Guesses about what a variable holds are wrong more often than you expect.
  2. Comment out half the code. Does the error persist? Now you know which half.
  3. Paste the function into Python Tutor and step through it. Watching variables change fixes most loop and list bugs in a minute.
  4. Write the smallest program that shows the problem. Often, by the time it is small, the bug is obvious.

When it runs but the answer is wrong

Ask well

On the discussion board or in office hours, share the exact error, the smallest code that causes it, and what you expected instead. That message gets answered in minutes; "my code does not work" does not.

Tip
Take a ten-minute walk. Nobody knows why it works, but the bug you could not see for an hour is often obvious when you come back.

Put it to work

Mathub turns these ideas into a daily plan: due flashcards on a spacing schedule, one-question lessons, endless practice and a focus timer, for M171, PHSX 220, M151Q, WRIT 101, CSCI 127, BIOB 160, KIN 322, PSYX 340.

More guides