how to handle zero division error python

Solutions on MaxInterview for how to handle zero division error python by the best coders in the world

showing results for - "how to handle zero division error python"
Idris
25 Jan 2017
1Python will display "ZeroDivisionError" if you divide something with zero
Abia
11 Nov 2018
1import decimal
2from enum import Enum
3
4from gw_utility.logging import Logging
5from mpmath import mpf
6
7
8class NumberType(Enum):
9    """Specifies number type or library used for calculating values."""
10    INTEGER = 1
11    FLOAT = 2
12    DECIMAL = 3
13    MPMATH = 4
14
15
16def main():
17    Logging.line_separator("FRACTION TEST", 40, '+')
18
19    divide_test(5, 25)
20
21    Logging.line_separator("WHOLE NUMBER TEST", 40, '+')
22
23    divide_test(25, 5)
24
25    Logging.line_separator("DIVIDE BY ZERO TEST", 40, '+')
26
27    divide_test(5, 0)
28
29
30def divide_test(denominator, numerator):
31    """Perform division tests using all different numeric types and mathematic libraries.
32
33    :param denominator: Denominator.
34    :param numerator: Numerator.
35    """
36    Logging.line_separator('as int')
37    Logging.log(divide(denominator, numerator))
38
39    Logging.line_separator('as float')
40    Logging.log(divide(denominator, numerator, NumberType.FLOAT))
41
42    Logging.line_separator('as decimal.Decimal')
43    Logging.log(divide(denominator, numerator, NumberType.DECIMAL))
44
45    Logging.line_separator('as mpmath.mpf')
46    Logging.log(divide(denominator, numerator, NumberType.MPMATH))
47
48
49def divide(numerator, denominator, lib: NumberType = NumberType.INTEGER):
50    """Get result of division of numerator and denominator, using passed numeric type or library.
51
52    :param numerator: Numerator.
53    :param denominator: Denominator.
54    :param lib: Type of numeric value or library to use for calculation.
55    :return: Division result.
56    """
57    try:
58        if lib == NumberType.INTEGER:
59            # Divide using standard integer.
60            return numerator / denominator
61        elif lib == NumberType.FLOAT:
62            # Convert to floats before division.
63            return float(numerator) / float(denominator)
64        elif lib == NumberType.DECIMAL:
65            # Divide the decimal.Decimal value.
66            return decimal.Decimal(numerator) / decimal.Decimal(denominator)
67        elif lib == NumberType.MPMATH:
68            # Divide using the mpmath.mpf (real float) value.
69            return mpf(numerator) / mpf(denominator)
70        else:
71            # Divide using standard integer (default).
72            return numerator / denominator
73    except ZeroDivisionError as error:
74        # Output expected ZeroDivisionErrors.
75        Logging.log_exception(error)
76    except Exception as exception:
77        # Output unexpected Exceptions.
78        Logging.log_exception(exception, False)
79
80
81if __name__ == "__main__":
82    main()
83
84
Lottie
23 Jul 2018
1x = 0
2try:
3  answer = 1/x
4except ZeroDivisionError:
5  answer = 'undefined'
6print('The answer is {result}'.format(result=answer)
7
queries leading to this page
python divide by zero errorhandling zero division error in pythondivide by zero pythonwhat happens if you divide by zero in pythonzerodivisionerror 3a division by zeropython zero division error ecan divide by zero how to python exceptiontry catch zero division error pythonexcept division by zero pythonzero division python try excepthow to handle divide by zero error in pythondivide by zero error pythonhandle zero division error pythonpython error handling divide by zerowrite a python program to perform zero division errorpython exception for zero divisiondivide by zero return 0 python and system exitpython how to return zero when divide by 0exception division by zero pythonpython except divide by zerotry catch division by zero pythonraise division by zero pythonhow to solve zero division error in pythonwhat does it mean to divide by zeroexception python division by zeropython division by zero is which type of error 3ftry except divide by zero pythonraise divide by zero pythonpython division by zero errorhow to avoid zero division error in pythonpython catching division by zeropython division by zero error when making functionexcept zero division error pythonpython zero division error exceptionhow to catch division by zero in pythonzero division error in pythonpython try except divide by zeronum divide by 0 error namepython handle divide by zerozerodivisionerror 3a division by zero pythonis there a way to divide a number by zero in pythonassert for zero division pythonexception divide by zero pythonpython error division by zerotraceback zero division error pythonerror division by zeropython if divide by zero then zerodivision by zero error pythonexcept divide by zero pythonhow to except division by zero in pythonwhat error happens in python when i divide by zerotest for zero division error pythonpython division by zero exceptionpython catch divide by zerodivide by zero is which type of error in pythondivision by zero errorzero division error try functionin pythonhow to get rid of divide by zero error in pythonpython giving division by zero error when it 27s notdivide by zero exception handling in pythonhow to fix divide by zero error in pythoncatch division by zero pythondivizion by error pythonpython error divide by zerozero division error pythonpython zero division errorpython handling division by zeropython throwing division by zero when there is nonewhat type of erroris division by zero in pythondivide by zero exception in pythondivide by zero return 0 python and exitdivide by zero exception example in pythontry except python divide by zerodivide by zero exception pythonpython zerodivisionerror 3a division by zerodivision by zero issue pythondivision by zero error in pythonexception for dividing by zero in pythondivide by zero error in pythonpython devide by 0 errorwhat happens when you divide a number by zero in pythonhandle divide by zero exception in pythonhandle divide by zero in pythonwhat error does dividing by zero give in pythonhow to handle zero division error pythoncode to handle division by zero pythonruntime error python divide by zerozero error pythonwhat type of error is division by zero in pythonpython divide by zero exceptiondivide by 0 error pythonpython exception divide by zerohow to handle division by zero in pythonpython divided by zero exceptionpython program to handle divide by zero exceptioncode to handle division by zero exception pythondivide by 0 with no error pythondivide by zero error python exception handlingpython divide by zerodivision by zero exception pythonprint cannot divide by zero if it is divide by zero pythonhow to import zero division error in pythonwhich of the following execption occurs 2c when a number is divided by zero 3fdivide by 0 what type of error pythonpython allow division by zerodivision by zero error pytjhonpython program to catch divide by zero exceptionpython code for error handling for a number divide by zeropython handle with divisionzero division error in python examplehow to give zero division error pythonavoid divide by zero exception in pythonhow to handle zero division error python