Safe Division Calculator
Write a function safe_divide(a, b) that returns the result of a / b, handling ZeroDivisionError by returning the message "Cannot divide by zero" instead of crashing.
What the problem means: wrap a risky operation (division, which can fail if b is 0) in a function that never crashes the caller, no matter what b is.
Approach: put the division inside a try block; catch ZeroDivisionError and return the fallback message instead.
Input: Two lines: a, then b.
Output: One line: the division result, or the message Cannot divide by zero.
10 0
Cannot divide by zero
- -10^9 <= a, b <= 10^9
Hint 1
Put a / b inside a try block.
Hint 2
except ZeroDivisionError: catches specifically the case where b is 0.
Hint 3
Return the string "Cannot divide by zero" from inside the except block.
Wrap the division in a try block inside safe_divide(). When b is 0, Python raises ZeroDivisionError, which the except block catches and turns into a plain, safe return value instead of letting the program crash. When b isn't 0, the division result (a plain float, from Python's / operator) is returned normally.