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
Jalapenos 85 points

                                    Python will display "ZeroDivisionError" if you divide something with zero

4 (1 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
what happens if you divide by zero in python how to avoid zero division error in python how to handle divide by zero error in python how to import zero division error in python divide by 0 error python divide by zero exception example in python what happens when you divide a number by zero in python what error does dividing by zero give in python runtime error python divide by zero code to handle division by zero exception python code to handle division by zero python python zero division error except division by zero python python exception divide by zero python allow division by zero what type of error is division by zero in python what type of erroris division by zero in python divide by zero is which type of error in python how to fix divide by zero error in python handling zero division error in python division by zero issue python try except divide by zero python divide by 0 what type of error python traceback zero division error python how to give zero division error python how to handle zero division error python raise divide by zero python divide by zero python exception division by zero python python how to return zero when divide by 0 python division by zero error when making function assert for zero division python test for zero division error python divide by zero error in python ZeroDivisionError: division by zero python how to handle division by zero in python division by zero exception python division by zero error Write a Python program to perform zero division error try except python divide by zero zero division error in python example python program to catch divide by zero exception python program to handle divide by zero exception how to get rid of divide by zero error in python python divided by zero exception divide by zero exception handling in python zero division error in python python code for error handling for a number divide by zero python division by zero exception how to catch division by zero in python how to solve zero division error in python what does it mean to divide by zero except zero division error python divide by zero exception python divide by zero exception in python python catch divide by zero python except divide by zero except divide by zero python python try except divide by zero division by zero error pytjhon exception divide by zero python can divide by zero how to python exception divide by zero error python exception handling python divide by zero error python Division by zero is which type of error? python error divide by zero DIVIZION BY ERROR python python divide by zero division by zero error in python divide by 0 with no error python Which of the following execption occurs, when a number is divided by zero? python divide by zero exception num divide by 0 error name python devide by 0 error handle divide by zero in python python division by zero error python error division by zero python handle with division ZeroDivisionError: division by zero zero division error python division by zero error python zero error python raise division by zero python divide by zero error python exception for dividing by zero in python what error happens in python when i divide by zero
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source