Module4_Answers_Complete
Module4_Answers_Complete
Q1. Explain the logging module and debug the factorial of a number program
1. The logging module provides a flexible framework for emitting log messages.
2. Logging is used to debug and track the execution flow of a program.
3. `basicConfig()` sets default format and level for logging.
4. Levels: DEBUG, INFO, WARNING, ERROR, CRITICAL.
5. Factorial is a recursive function; logging helps track each call.
6. `logging.debug()` used inside recursion to show computation.
7. Helps diagnose logic errors and trace program behavior.
8. Example:
import logging
logging.basicConfig(level=logging.DEBUG)
def factorial(n):
logging.debug(f"Calculating factorial({n})")
return 1 if n == 0 else n * factorial(n - 1)
print(factorial(5))