+ 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

22nd Nov 2018, 1:16 PM
Shams Karimi
Shams Karimi - avatar
4 Respostas
+ 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"
22nd Nov 2018, 1:21 PM
Matthias
Matthias - avatar
+ 3
Forgot to mention the case when num_1 is negative. Then it should be -inf of course 😅
22nd Nov 2018, 4:34 PM
Matthias
Matthias - avatar
+ 2
Matthias thanks for the help, it worked! :)
22nd Nov 2018, 2:17 PM
Shams Karimi
Shams Karimi - avatar
23rd Nov 2018, 4:57 PM
Seniru
Seniru - avatar