To solve ZeroDivisionError: division by zero, use the try-except statement. The try block lets you test a block of code for errors. The except block enables you to handle the error.
ZeroDivisionError: division by zero in Python
ZeroDivisionError is a built-in Python exception thrown when a number is divided by 0. As a common mathematics rule, a number divided by 0 is infinity. Therefore, this error may cause even when a number is modulo by 0. The ZeroDivisionError can be handled by exception handlers.
We can put the block of code that can cause an error into the try block and test the code. For example, in the exception block name, the exception is ZeroDivisionError the exception will be executed if the number is divided by zero.
a = int(input("enter the value for a: ")) b = int(input("enter the value for b: ")) c = a / b print(c)
enter the value for a: 10 enter the value for b: 0 Traceback (most recent call last): File "/Users/krunallathiya/Desktop/Code/R/data.py", line 4, in <module> c = a / b ZeroDivisionError: division by zero
In this example, let us consider the value for variable a = 10 and the value for b = 0. When the program gets executed, the error is raised. The error raised is called the ZeroDivisionError.
If the value of b is given as 2, the program would have executed successfully. Even when the value of a is 0, the program works fine and prints 0 as output.
But if we pass b = 0, it creates an error. Only during division operation is performed is this error raised.
Solve ZeroDivisionError: division by zero using if
Use the if statement to solve the ZeroDivisionError in Python. You can check the value of the denominator using the if statement if you divide a number by a denominator value. If the denominator value is zero, we execute the else statement; otherwise, it will execute the if statement.
a = int(input("enter the value for a: ")) b = int(input("enter the value for b: ")) if(b > 0): c = a / b print(c) else: print("The value of b should be greater than 0")
enter the value for a: 10 enter the value for b: 0 The value of b should be greater than 0
Use try-except to solve ZeroDivisionError
The try-except approach can be used to handle the exception. The ZeroDivisionError occurs when we divide numbers and find the denominator 0.
Using the try-except approach, we are putting division code inside the try block, and if it finds an exception, then except block will be executed.
a = int(input("enter the value for a: ")) b = int(input("enter the value for b: ")) try: c = a / b except ZeroDivisionError: print("The value of b should not be equal to 0 please change the value for b") c = 0 print(c)
enter the value for a: 10 enter the value for b: 0 The value of b should not be equal to 0 please change the value for b 0
If you encounter ZeroDivisionError in Python, it suggests that there is a number dividing by zero or the denominator’s value is zero. To handle this exception, use the try-except block and handle the exception, and you can set the output as 0, which is optional.
That’s it for this tutorial.