Sep 7, 2026
4 min read
Python bugs feel harder when you try to debug the whole program at once. Before changing code, make the failure smaller. Copy the smallest input that triggers the problem, run only the function involved if possible, and remove unrelated print statements or UI code. A smaller failure gives you fewer places to search.
If your program reads a file, create a tiny test file with two or three rows. If your program processes a list, test with one item, an empty list, and a normal list. If your program calls an API, save one sample response and test your parsing logic against that fixed data.
Python tracebacks show the path your program took before it failed. The last few lines usually matter most:
ValueError: invalid literal for int() with base 10: 'N/A'
That error is not saying int() is broken. It is saying your code tried to convert a value that is not a valid integer. The next question is specific: where did 'N/A' come from, and should that value be skipped, cleaned, or handled separately?
Use this order:
Many students add prints like this:
print("here")
print("working")
print("broken")
Those messages confirm where execution reached, but they do not show why the logic failed. Print the actual values and types:
print("raw_score:", raw_score, type(raw_score))
print("parsed_score:", parsed_score, type(parsed_score))
Types matter in Python because "10" and 10 look similar when printed, but behave differently in comparisons, arithmetic, sorting, and JSON output.
A correct algorithm can still produce bad results if the inputs are not what you think they are. Before rewriting a function, inspect the data entering it.
Common input surprises:
| Surprise | Example | Fix |
|---|---|---|
| Extra whitespace | " Alice " | Use .strip() |
| Wrong type | "42" instead of 42 | Convert once at the boundary |
| Missing value | None or "" | Add a clear fallback or validation error |
| Different casing | "CS" vs "cs" | Normalize with .lower() where appropriate |
| Unexpected nesting | {"data": {"items": [...]}} | Print keys before indexing |
When a bug appears deep in the program, trace one value backward until you find where it first became wrong.
Do not wait until the full program runs to test edge cases. Write tiny checks for the behavior that usually breaks:
def average(scores):
if not scores:
return 0
return sum(scores) / len(scores)
print(average([80, 90, 100])) # normal case
print(average([100])) # single item
print(average([])) # empty input
Edge cases are not rare in grading scripts, data-processing tasks, or backend code. They are often exactly what tests are designed to catch.
Some Python errors have nothing to do with your function logic. If code works in one terminal but fails in another, check the environment.
Run:
python --version
python -m pip --version
python -m pip list
If you are using a virtual environment, activate it before installing packages and before running the program. Installing a package globally while running the project inside a different environment is a common reason for ModuleNotFoundError.
Assertions are useful when you believe something must be true:
assert isinstance(scores, list)
assert all(isinstance(score, int) for score in scores)
They help you catch wrong assumptions close to the source. For production code, you may replace assertions with explicit validation and helpful error messages, but while learning and debugging, assertions are a fast way to locate the moment your mental model diverges from the program.
Random edits make bugs harder to solve because you lose track of what changed. If you have tried three fixes and the error is still unclear, pause and write down:
That short note often reveals the next test to run.
Python debugging is a skill, not a guessing game. If you are stuck on a traceback, a data-shape problem, or logic that returns almost-correct results, EduSupport's Python tutoring and student code review can help you inspect student-written code, understand the error, and decide what to revise next.
Discuss tutoring, code review, project mentoring, or research-method guidance.