← Back to Education
Education digital-technologies year-9

Chapter 10 - Debugging

A lesson on reading tracebacks, isolating problems, and using a reliable debugging routine.

Published Updated Type lesson

Chapter 10 treats errors as information. It gives students a method for debugging instead of random guessing.

Chapter Navigation

Chapter 10 — Final Build and Debugging
EXPLORER · v10 · understanding how programs break and how to fix them
You have built a complete program
Your console now collects input, converts data types correctly, makes decisions using conditional statements, repeats using a loop, stores multiple reports in a list, and organises behaviour using functions. That is a structured, multi-part program.

The final skill to develop is not adding new features. It is learning how to stay calm when something breaks and work through the problem logically.
EXPLORER v10 — Final Version
"""
EXPLORER v10 — Final Build
"""

def print_banner():
    print("=" * 44)
    print("              EXPLORER CONSOLE              ")
    print("=" * 44)

def create_report():
    mission_location = input("Enter mission location: ")
    mission_day = int(input("Enter mission day (whole number): "))
    temperature_c = float(input("Enter temperature (°C): "))

    if temperature_c > 35:
        condition = "Unsafe"
    elif temperature_c > 25:
        condition = "Warm"
    else:
        condition = "Normal"

    report_summary = (
        "Location: " + mission_location +
        " | Day: " + str(mission_day) +
        " | Temp: " + str(temperature_c) + " °C" +
        " | Condition: " + condition
    )

    return report_summary

reports = []
keep_running = True

while keep_running:

    print_banner()

    report = create_report()
    reports.append(report)

    print()
    print("Report logged.")
    print()

    user_choice = input("Log another report? (yes/no): ")

    if user_choice.lower() != "yes":
        keep_running = False

print()
print("All logged reports:")
print("-" * 44)

for report in reports:
    print(report)

print("-" * 44)
print("Console shutting down.")
Reading tracebacks properly
When Python encounters a problem, it prints a traceback. Many students read only the top line and become overwhelmed. Instead, train yourself to read from the bottom upward.

The final line tells you the type of error. The lines above it tell you where it happened. The file name and line number point directly to the location in your code.

Traceback (most recent call last):
  File "explorer.py", line 18, in <module>
    mission_day = int(input("Enter mission day: "))
ValueError: invalid literal for int() with base 10: 'two'
Start at the bottom. The error is ValueError. That tells you the issue involves a value that cannot be converted. The message explains that the word "two" cannot be turned into an integer.

Next, look at the line number. Go to that exact line in your file. Do not rewrite the entire program. Inspect that single line first.
Indentation defines structure
In Python, indentation is not decoration. It defines which lines belong together. Lines indented under a while, if, or def statement are part of that block.

Moving a line left or right changes whether it runs inside or outside the block.

while keep_running:
    print("Inside loop")
    keep_running = False
Here, both lines are inside the loop. The variable changes inside the loop, so the loop runs once and stops.

while keep_running:
    print("Inside loop")
keep_running = False
In this version, the second line is outside the loop. The loop never changes the condition and runs forever. The indentation changed the logic of the program.

This is why careful spacing matters.
Common beginner errors with real examples
The fastest way to get good at debugging is to recognise patterns. Most beginner errors are the same few types, just in different outfits. In each example below, notice two things: where Python points (the line number) and what the error name is (the last line). Those two clues usually get you 80% of the way to the fix.
IndentationError
This one is about structure. Python uses indentation to decide what belongs inside a loop, inside an if statement, or inside a function. If the indentation is inconsistent, Python can’t safely guess what you meant.
Broken example
if True:
    print("This line is indented")
  print("This line is not")
What you might see
IndentationError: unindent does not match any outer indentation level
Fixed version
if True:
    print("This line is indented")
    print("This line is also indented")
The fix is almost always the same: make sure every line in the same block has the same indentation. If you’re using spaces, stick with spaces. If you press Tab sometimes and spaces other times, Python will eventually punish you for it.
SyntaxError
Syntax means “grammar.” A SyntaxError means Python can’t even read the code as a valid sentence. The most common causes are missing colons, missing brackets, or quotes that never close.
Broken example (missing colon)
if temperature_c > 35
    print("Unsafe")
What you might see
SyntaxError: expected ':'
Fixed version
if temperature_c > 35:
    print("Unsafe")
If you see SyntaxError, slow down and look for a missing piece of punctuation. Python is extremely literal. One missing colon or quote can break the whole program.
NameError
NameError means Python does not recognise a variable name you used. Usually it’s because the variable was never created, or because the spelling doesn’t match exactly.
Broken example (spelling mismatch)
mission_location = "Crater Ridge"
print(mission_loction)
What you might see
NameError: name 'mission_loction' is not defined
Fixed version
mission_location = "Crater Ridge"
print(mission_location)
Python is case-sensitive too, which means mission_day and Mission_Day are different names. If you see NameError, check spelling first, then check whether the variable is created before you use it.
TypeError
TypeError is what happens when Python understands your instructions, but the data types you used don’t match what the instruction requires. The classic beginner version is mixing strings and numbers.
Broken example (string + int)
mission_day = 3
print("Day: " + mission_day)
What you might see
TypeError: can only concatenate str (not "int") to str
Fixed version (convert to string)
mission_day = 3
print("Day: " + str(mission_day))
This is the “types matter” lesson. If Python says a string and an int can’t be combined, believe it. Convert the type explicitly.
ValueError
ValueError often appears when you try to convert input using int() or float(), but the user typed something that can’t become a number.
Broken example (bad input for int)
mission_day = int(input("Enter mission day: "))
If the user types two instead of 2, Python will crash because int("two") is not possible.
What you might see
ValueError: invalid literal for int() with base 10: 'two'
This is a great moment to remember: errors don’t always mean your code is “wrong.” Sometimes it means the input is not what your code expects.
IndexError (lists)
Once you start using lists, this one appears. IndexError means you tried to access a position that does not exist in the list.
Broken example
reports = []
print(reports[0])
What you might see
IndexError: list index out of range
An empty list has no item at position 0. The fix is not “try harder.” The fix is “check whether the list has anything in it before you access it,” or only access items you know exist.
ZeroDivisionError
This one is simple, but it surprises beginners because it feels like “math should handle this.” Python refuses because division by zero is undefined.
Broken example
total = 10
count = 0
average = total / count
print(average)
What you might see
ZeroDivisionError: division by zero
The fix is to protect the calculation with an if statement so it only runs when the number you divide by is not zero.
My debugging routine (feel free to use this for yourself)
When your code crashes, don’t start deleting random lines. Debugging works best when you follow the same routine every single time, like a checklist for your brain.
  1. Start at the bottom of the error message. The last line tells you the error type (SyntaxError, NameError, TypeError, ValueError, etc.). That name is your first clue about what kind of mistake it is.
  2. Find the line number. The traceback shows a file name and a line number. Go straight to that line in your code. Don’t guess. Don’t rewrite the whole program.
  3. Explain that line in plain English to yourself. What is it trying to do? Convert input? Print a value? Use a variable? Access a list item? Most bugs become obvious when you calmly describe what the line is meant to achieve.
  4. Check the “usual suspects” for that error type.
    If it’s a SyntaxError, look for missing colons, brackets, or quotes that never close.
    If it’s a NameError, check spelling and capital letters (Python is case-sensitive).
    If it’s a TypeError, check whether you’re mixing types (string vs int/float) without converting.
    If it’s a ValueError, check whether the input can actually be converted into the type you asked for.
    If it’s an IndentationError, zoom out and check whether that line is meant to be inside or outside a loop/if/function.
  5. If indentation is involved, check the structure, not just the line. Ask: “What block is this line meant to belong to?” Moving a line left or right can change whether it runs once, runs repeatedly, or never runs at all.
  6. Make ONE small change. Fix one thing, then run the code again. If you change five things at once, you’ll never know what actually fixed it.
  7. Run it again and re-check the message. If there’s still an error, repeat the routine. Debugging is just looping with a purpose.
Now go break your code, fix it, and pretend it was all part of your plan! 😄
Mr Bhattaram