THE ONLYPREPY JOURNAL Coding
How to debug your code when you feel stuck
A beginner-friendly way to debug code: reproduce the problem, inspect the values, test one explanation and check the fix. Includes a short Python example.
THE IDEA TO TAKE WITH YOU
Make the problem small enough to observe. Change one thing for a reason, then check that your explanation fits what the program actually does.
When code does not work, it is tempting to change a few lines and run it again. Sometimes the output changes, but you still do not know why. The next problem can then feel just as confusing.
Debugging becomes easier to follow when each attempt answers a small question. What did you expect? What actually happened? Where do the two begin to differ? You can use that sequence with a short school exercise as well as a larger personal project.
Describe the expected result
Choose one input and write down the output you expect before running the program. Keep the example small enough to calculate by hand. If the task is to add a list of numbers, use two or three numbers rather than a large file of data.
Now record what the program actually does. Does it stop with an error? Produce the wrong value? Repeat something too many times? The exact error message and the relevant line are useful evidence. Read them before searching for a solution.
If you are unsure what the program should do, return to the task description first. You need a clear expected result before you can decide whether a change has fixed the problem.
Make the problem repeatable
Run the same input again. A problem that happens consistently is easier to investigate because you can compare the effect of one change at a time.
Remove unrelated features from a copy of the program, or make a small separate example that shows the same behaviour. Keep your original work saved. Use sample values rather than private data, and remove passwords or access tokens before sharing code with anyone.
For a school assignment, follow the rules about outside help. A tutor can discuss your reasoning and debugging process, while the assessed work remains your own.
Trace the values through the code
Consider this Python function:
def total_scores(scores):
total = 0
for score in scores:
total = score
return total
print(total_scores([4, 7, 2]))
You expect 13, but the function returns 2. Follow the variable total through each loop iteration. It begins at 0, becomes 4, then 7, then 2. The line total = score replaces the previous value each time.
The issue is not the final print statement. It is the operation used to update the running total. Writing the intermediate values on paper, using a debugger or temporarily printing them can help you locate that difference.
Keep these observations focused. Printing every variable in a large program can produce more output than you can use. Start near the first point where the actual value differs from your expectation.
Test one explanation at a time
State a prediction before editing: “If the loop adds each score to the existing total instead of replacing it, this input should produce 13.” Then change the relevant line:
def total_scores(scores):
total = 0
for score in scores:
total += score
return total
Run the original input again. If it now produces 13, you have evidence for that explanation. If it does not, inspect what happened before adding another change.
This habit helps distinguish a fix you understand from an accidental improvement. When a change does not help, undo it or record it clearly so you do not lose track of which version you are testing.
Check more than the first example
A single successful run is a useful start, but it does not cover every input. For this function, try an empty list, a one-item list and a list containing a negative number. Work out the expected answer for each before running it.
You could use these small checks in Python:
assert total_scores([]) == 0
assert total_scores([5]) == 5
assert total_scores([4, 7, 2]) == 13
assert total_scores([4, -2]) == 2
These examples assume that the input contains numbers. If your task allows other input types, decide how those should be handled and test that behaviour separately. Read the task requirements rather than adding rules the program was never asked to support.
Ask for help with useful evidence
If you are still stuck, prepare a short explanation containing the smallest relevant code sample, the input, the expected result, the actual result and the changes you have already tested. Say which step you do not understand.
That makes a conversation with a teacher, tutor or classmate much easier to follow. It also gives you another opportunity to explain the problem yourself. Avoid sharing an entire project when a few lines demonstrate the issue.
For guided practice with tracing, testing and writing your own programs, explore coding and programming tutoring. Our first-lesson checklist can help you bring a useful example and a clear question.
One question at a time.
Talk to us about your learning →