Logging-Enhanced Divide Function
Add logging (INFO for a successful division, ERROR for a failed one) to a function that divides two numbers, replacing what would otherwise be print() statements.
Approach: configure logging to print to the console (stdout) at INFO level, then have safe_divide(a, b) log an ERROR and return None when b is 0, or log an INFO message with the result otherwise.
Input: Two lines: a, then b.
Output: One line: the logged INFO message if b != 0, or the logged ERROR message if b == 0.
10 2
INFO - Divided 10 by 2 = 5.0
- -10^6 <= a, b <= 10^6
Hint 1
stream=sys.stdout in basicConfig is important — by default logging writes to stderr, which wouldn't show up as normal program output.
Hint 2
logging.error("...") and logging.info("...") are the two calls this function needs, one per branch.
Hint 3
The format "%(levelname)s - %(message)s" is what turns each log call into a line like "INFO - ...".
safe_divide checks b first: if it's 0, logging.error(...) records the failure and the function returns None; otherwise it computes the result and logging.info(...) records the successful operation. Configuring basicConfig with stream=sys.stdout (logging defaults to stderr otherwise) is what makes the log line show up as ordinary program output.