how to handle zero division error python
import decimal
from enum import Enum
from gw_utility.logging import Logging
from mpmath import mpf
class NumberType(Enum):
"""Specifies number type or library used for calculating values."""
INTEGER = 1
FLOAT = 2
DECIMAL = 3
MPMATH = 4
def main():
Logging.line_separator("FRACTION TEST", 40, '+')
divide_test(5, 25)
Logging.line_separator("WHOLE NUMBER TEST", 40, '+')
divide_test(25, 5)
Logging.line_separator("DIVIDE BY ZERO TEST", 40, '+')
divide_test(5, 0)
def divide_test(denominator, numerator):
"""Perform division tests using all different numeric types and mathematic libraries.
:param denominator: Denominator.
:param numerator: Numerator.
"""
Logging.line_separator('as int')
Logging.log(divide(denominator, numerator))
Logging.line_separator('as float')
Logging.log(divide(denominator, numerator, NumberType.FLOAT))
Logging.line_separator('as decimal.Decimal')
Logging.log(divide(denominator, numerator, NumberType.DECIMAL))
Logging.line_separator('as mpmath.mpf')
Logging.log(divide(denominator, numerator, NumberType.MPMATH))
def divide(numerator, denominator, lib: NumberType = NumberType.INTEGER):
"""Get result of division of numerator and denominator, using passed numeric type or library.
:param numerator: Numerator.
:param denominator: Denominator.
:param lib: Type of numeric value or library to use for calculation.
:return: Division result.
"""
try:
if lib == NumberType.INTEGER:
# Divide using standard integer.
return numerator / denominator
elif lib == NumberType.FLOAT:
# Convert to floats before division.
return float(numerator) / float(denominator)
elif lib == NumberType.DECIMAL:
# Divide the decimal.Decimal value.
return decimal.Decimal(numerator) / decimal.Decimal(denominator)
elif lib == NumberType.MPMATH:
# Divide using the mpmath.mpf (real float) value.
return mpf(numerator) / mpf(denominator)
else:
# Divide using standard integer (default).
return numerator / denominator
except ZeroDivisionError as error:
# Output expected ZeroDivisionErrors.
Logging.log_exception(error)
except Exception as exception:
# Output unexpected Exceptions.
Logging.log_exception(exception, False)
if __name__ == "__main__":
main()
4
1
Python will display "ZeroDivisionError" if you divide something with zero
Thank you!
1
0
Are there any code examples left?
New code examples in category Python
-
Python 2023-04-11 03:04:20
-
Python 2022-03-27 22:40:04 pycharm no module named
-
Python 2022-03-27 22:25:05 assign multiple variablesin one line
-
Python 2022-03-27 22:20:02 levenshtein distance
-
Python 2022-03-27 21:35:09 get text from url python last slash
-
Python 2022-03-27 21:30:30 df concatenate df
-
Python 2022-03-27 21:25:09 python odd or even
-
Python 2022-03-27 21:15:32 python include function from another file
-
Python 2022-03-27 21:10:01 color module python
-
Python 2022-03-27 21:00:27 python tkinter cursor types