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
- The last line of a Python traceback names the error and usually the cause: NameError means a typo or something defined later; TypeError often means a string where a number was expected; IndexError means you asked for an item that is not there.
- The line above it with "File ... line N" points at your code. Start there, not at the top of the file.
- Paste the exact message into a search engine in quotes. Someone has hit it before.
Make it smaller
- 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.
- Comment out half the code. Does the error persist? Now you know which half.
- Paste the function into Python Tutor and step through it. Watching variables change fixes most loop and list bugs in a minute.
- 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
- Hand-trace the code with a tiny input (a list of three numbers) on paper.
- Check the boundaries: the first item, the last item, an empty list, zero.
- Check integer division and off-by-one in ranges: `range(1, n)` stops at n - 1.
- Use the playground in Mathub to test one function at a time with known inputs.
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.
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
How to study for a math exam: the ten-day plan
Rereading notes the night before is the most common plan and the worst one. Here is what works, day by day.
Active recall and spaced repetition: why rereading feels good and does not work
The two most-researched study techniques, explained in five minutes, with how Mathub uses them.
The 25-minute block: how to actually start
Starting is the hard part. A short timer, a single task and a visible finish line get you past it.
How to read a math or physics section in twenty minutes
Textbooks are not novels. Read them out of order, with a pencil, and stop when you have what you need.