Problem C
Safe Division
Write a program that prompts for two floating point numbers. Then call a function named divide_safe(num1_str, num2_str), which returns the result of dividing the first number by the second number. If the given strings do not represent floating point numbers or the second number is 0, the function returns None. Use try-except and catch two types of exceptions: ValueError and ZeroDivisionError.
The main program prints the result rounded to 5 decimal digits if no error occurred, else prints None.
Input
The input consists of:
-
A string
representing the first number. -
A string
representing the second number.
Note that the strings may contain symbols which are not
digits. Each line will contain at most
Output
The output consists of:
-
The result of the division rounded to 5 decimal digits or None in case of an error.
Sample Input 1 | Sample Output 1 |
---|---|
23.3 4.6 |
5.06522 |
Sample Input 2 | Sample Output 2 |
---|---|
5.6z 3.4 |
None |
Sample Input 3 | Sample Output 3 |
---|---|
5.34 0 |
None |
Sample Input 4 | Sample Output 4 |
---|---|
34.56 abc |
None |
Sample Input 5 | Sample Output 5 |
---|---|
123.45 8974.752 |
0.01376 |
Sample Input 6 | Sample Output 6 |
---|---|
5 2 |
2.5 |