Debugging and logging
Debugging means finding and fixing errors. Start by reading the error message (the traceback) carefully: the last line gives the type of error, and the lines above show where it happened. Then check variable values with print(), or with a debugger such as breakpoint() or the one in VS Code, which lets you run the code line by line.
In a real program, you replace debugging print() calls with the logging module, which sorts messages by severity: DEBUG, INFO, WARNING, ERROR, CRITICAL. You choose the minimum level to show and the message format, and you can write them to a file.
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format="%(levelname)s: %(message)s", force=True)
def divide(a, b):
logging.debug(f"divide({a}, {b})")
if b == 0:
logging.error("Division by zero!")
return None
return a / b
print(divide(10, 2))
print(divide(1, 0))