+ 3
How to handle ‘ZeroDivisionError’ in Python?
Yesterday I programmed a simple calculator(link in below) but when I divide a number (integer/float) by zero, it gives an error, please help me handle that. Thanks https://code.sololearn.com/cDlTPy8oRYKo/?ref=app
4 Antworten
+ 10
Well, devision by zero is undefined...
But if you want to look at it as a limit, where division by zero "is" infinity, you could use a try/except:
try:
result = num_1 / num_2
except ZeroDivisionError:
result = "inf"
+ 3
Forgot to mention the case when num_1 is negative. Then it should be -inf of course 😅
+ 2
Matthias thanks for the help, it worked! :)
+ 1
I think this will help - https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2441/