๐Ÿ›
๐Ÿ› Debugging & Problem-Solving ยท DEBUG-101 ยท Session 3 of 6

Print, Trace, and See What's Happening

Not every bug announces itself with an error โ€” sometimes code runs fine but does the wrong thing. The fix is to make the invisible visible: see what your program is actually doing.

โฑ 8 min read๐Ÿ“… Updated 2026-07-30๐Ÿ“Š Tables, charts & examples

Some bugs crash with an error - those are the easy ones. The trickier bugs are when code runs without complaint but produces the wrong answer. There's no traceback pointing at a line, so how do you find it? The answer is to make the invisible visible: look at what your program is actually doing, step by step. The humble print statement is the most-used debugging tool in the world - and tools like larztrace take it further.

When you don't know why code misbehaves, print out what it's actually doing - the values of variables at key points. Reality often differs from what you assumed.

def total_price(items):
    total = 0
    for item in items:
        print("adding:", item, "running total:", total)   # <- see it happen
        total += item["price"]
    return total
๐ŸŽฏ
Print your assumptions and check them against reality. Most bugs come from a gap between what you think the code is doing and what it's actually doing. A print statement closes that gap: print the variable you assume holds a certain value, and you'll often discover it holds something else - an empty list, a string where you expected a number, a loop running one time too many. You don't need fancy tools to start; strategic prints have found more bugs than every debugger combined. Add them, run, read, then remove them.

Trace: watch it run

Printing shows you specific spots; sometimes you want to watch the whole flow - every line and every change. That's what a tracer does.

๐Ÿ’ก
The stack's larztrace runs your code and records every line as it executes and every variable as it changes - so you can watch the program's whole story unfold instead of guessing. It's the same idea as print statements, turned up to eleven: total visibility. Watching a loop run step by step, seeing exactly when a variable becomes the wrong value, is often the fastest way to catch a logic bug that never raises an error.

Make the invisible visible

  • Error-free wrong answers are found by seeing what the code actually does.
  • print() is the universal debugger - show variables at key points.
  • Check assumptions - bugs live in the gap between assumed and actual behaviour.
  • larztrace watches every line and variable change - print turned up to eleven.

๐Ÿงฉ Do it with LarzOS

Turn this lesson into action with the exact free tools and AI workflows for the job:

๐ŸŽฏ Key takeaways

  • Some bugs run without errors but produce wrong answers - no traceback to guide you.
  • Make the invisible visible: look at what your program is actually doing, step by step.
  • print() is the world's most-used debugger - show the values of variables at key points.
  • Bugs hide in the gap between what you assume the code does and what it actually does; larztrace shows the whole flow.

Frequently asked

Is using print statements to debug amateurish? Shouldn't I use a real debugger?

Not at all - print debugging is used constantly by programmers at every level, including experts at top companies, and there's no shame in it whatsoever. It's popular for good reasons: it's immediate (no setup), it works everywhere (any language, any environment), and it's perfectly suited to the core debugging question of 'what value does this variable actually have right now?'. Dedicated debuggers (tools that let you pause a program and step through it) are genuinely powerful and worth learning eventually - they let you inspect everything at a paused moment without editing code - but they're a complement to print debugging, not a replacement, and many seasoned developers reach for a print statement first because it's faster for most everyday bugs. So don't feel you must master a fancy debugger to be legitimate; strategic print statements will serve you well for a long time and remain useful forever. The skill that matters isn't which tool you use - it's the habit of making the program's actual behaviour visible and checking it against your assumptions. Start with print, add a tracer like larztrace to see fuller flow, and pick up a step debugger when you're ready; all are valid, and print is a real programmer's tool.

Where should I put print statements to find a bug efficiently?

Put them where your assumptions live - at the points where you believe something specific is true, so you can check whether it actually is. A good strategy is to bracket the problem: print a variable's value before and after the section you suspect, so you can see whether it changed as expected. If a loop is misbehaving, print inside the loop each iteration to watch how the values evolve (you'll often spot it running one time too many, or a value drifting wrong). If a function returns something surprising, print its inputs at the top and its result at the bottom. The efficient approach is a kind of binary search: rather than sprinkling prints everywhere, place a couple to narrow down which half of the code the problem is in, then move them closer to the exact spot based on what you learn - if the value is already wrong at the first print, the bug is earlier; if it's still right there but wrong later, it's in between. Print the specific things you're unsure about (the value, the type, the length of a list, whether a branch runs), run, read, and let each result tell you where to look next. And remember to remove them once you've found the bug. This bracket-and-narrow method finds bugs far faster than random changes, and it's exactly the isolation skill the next session builds on.

Session 3 of 6 complete

Next up: Isolate the Problem

Continue to Session 4 โ†’

Related lessons

More in this course

Course home → · Browse all courses →