easyUnit 10 · Modules, Exception Handling & File Handling
P28. Safe Division
Modules, Exception Handling & File Handling
Read two values and divide the first by the second. Use try-except-else-finally:
| Situation | |
|---|---|
A value is not a whole number (ValueError) | Invalid input |
The second number is 0 (ZeroDivisionError) | Cannot divide by zero |
No error (else block) | Result: <a / b> rounded to 2 decimal places |
Always (finally block) | Done |
Input Format
- Line 1: the first value
- Line 2: the second value
Output Format
- Line 1: the result or the error message
- Line 2:
Done
Constraints
- Each line has at most 20 characters
Sample Test Cases
Sample 1
Input
10 4
Output
Result: 2.50 Done
Sample 2
Input
10 0
Output
Cannot divide by zero Done
Submitting checks 2 sample cases and 4 hidden test cases.