Debugging is the process of locating and correcting a defect. Begin with a specific difference between expected and observed behavior, then inspect the steps that could produce it.
A function should add a handling fee of 3 to a base cost. This version accidentally subtracts it:
def total_cost(base):
fee = 3
total = base - fee
print("base:", base, "fee:", fee, "total:", total)
return total
print("returned:", total_cost(10))
base: 10 fee: 3 total: 7
returned: 7
The expected result is 13. The intermediate values show base and fee are as intended, but subtraction produces 7. Change the operation to addition and remove the temporary diagnostic print:
def total_cost(base):
fee = 3
total = base + fee
return total
print(total_cost(10))
13
An investigation can keep input fixed, change one suspected cause at a time, then rerun the case and relevant tests. Add a test for the defect so it is less likely to return unnoticed. Avoid printing passwords, tokens, or private data in real programs.