handling zero division error in python

Solutions on MaxInterview for handling zero division error in python by the best coders in the world

showing results for - "handling zero division error in python"
Anton
03 Jan 2018
1try:
2    print 1/0
3except ZeroDivisionError:
4    print "You can't divide by zero!"
5
Maja
10 Mar 2020
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
Steven
23 May 2020
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
code to handle division by zero pythonzerodivisionerror 3a division by zero numpy meanwhich exception handles the error of divide by zero 3fpython divided by zero exceptionpython divide by zero exceptionpython allow division by zerodivide by zero error python exception handlingpython zerodivisionerror 3a division by zerodivision by zero exception pythonpython program to catch divide by zero exception finally block divide by 0 exception pythonruntime error python divide by zerodivide by zero error in pythontry except divide by zero pythondivision by zero errorhow to solve zero division error in pythonassert for zero division pythondivide by zero exception in pythonzero division error pythondivision by zero python errorzerodivisionerror 3a division by zero pythonexception division by zero pythonpython throwing division by zero when there is nonewhat type of error is division by zero in pythonpython zero division error exceptiondivide by zero exception namehow to import zero division error in pythontry except python divide by zerodivide by zero is which type of error in pythondivide by zero exception pythondivide by zero error pythonpython code for error handling for a number divide by zeropython error handling divide by zeropython giving division by zero error when it 27s notzero division error in pythonwhat error does dividing by zero give in pythonexception python division by zeropython zero division errorpython program to catch divide by zero exceptionexcept zero division error pythonzero division error in python examplepython catching division by zerodivision by zero error pythonexcept division by zero pythonpython program to handle divide by zero exceptionpython try catch not catching runtime divide by zerohandling zero division error in pythonexception for divide by zerohandle zero division error pythonerror division by zerotraceback zero division error pythonexception handling in python divide by 0except divide by zero pythoncatch division by zero pythonpython divide by zero errorhow to avoid zero division error in pythonhow to handle zero division error pythonpython program to catch divide by zero exception finally block toopython division by zero error when making functionexception handling for divide by zerodivide by zero exceptionpython built in error divide by zerocode to handle division by zero exception pythonpython error division by zeropython division by zero exceptiondivide by zero error catch pythonhow to catch division by zero in pythonpython exception for zero divisionwrite a python program to perform zero division errordivision by zero python error mlpython zero division error ehow to give zero division error pythonzerodivisionerror 3a float division by zero python with try 3a exceptcatch divide by zero exception pythonexception divide by zero pythondivide by zero exception example in pythonhow to handle division by zero in pythontest for zero division error pythonhow to get rid of divide by zero error in pythoncatch divide by zero error pythondivision by zero error python pandasexception when zero divide thentry catch division by zero pythonhow to fix divide by zero error in pythonpython division by zero errordivision by zero error in pythonpython try except divide by zerodivide by zero exception handling in pythonpython handling division by zerozero division python try exceptpython error type zerodivisionerror division by zero can divide by zero how to python exceptiontry catch zero division error pythonpython exception divide by zerohandle divide by zero exception in pythondivision by zero issue pythonavoid divide by zero exception in pythonhow to handle divide by zero error in pythonexception handling divide by zero with number format exceptionwhat type of erroris division by zero in pythonzero division error try functionin pythonpython return error division by zerohow to conitnue after divide by zero error pythonhandling zero division error in python