0
How to handle zero by zero exception in python?
5 ответов
+ 2
Divyansh Upadhyay
Please review the Exception chapter, the example there uses division by zero case for the code sample.
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2441/
+ 2
Sometimes you can also just evade the problem with a condition.
Let's say you want to divide every number in a list by 2.
numbers = [5, 7, 0, 3, 2]
for i in range(5):
if numbers[i]:
numbers[i] /= 2
+ 1
scr4pp3r You should try to be as specific as possible when catching an error so that other errors don't get caught accidentally. That is why Williams code is preferable to yours.
0
try:
x = 0/0
print x
except Exception as e:
print e
run this code n you will get it
0
user = int(input("Type you calculation"))
try:
user/0
except ZeroDivisionError:
print("Can not divide by zeor")