+ 6

Python Exceptions

How come if I change the '0' to 'input' it doesn't come up with the "divided by zero error" try: print("Hello") print(1 / input) except ZeroDivisionError: print("Divided by zero") finally: print("This code will run no matter what")

30th Dec 2018, 11:52 AM
Matt Harrison
Matt Harrison - avatar
4 Réponses
+ 7
Do you mean, you input 0 and it doesn't work like you expected? Input is always in string format, so you're not dividing by 0, but by '0', which also doesn't work, but for other reasons. You also (at least here) do not call the input function, because the parentheses after input are missing. So you're dividing by a function name which still doesn't work, again for other reasons. Change the line to print(1/int(input())) and you should get your ZeroDivisionError again (as long as you input zero).
30th Dec 2018, 12:07 PM
HonFu
HonFu - avatar
+ 3
HonFu Thanks mate!
30th Dec 2018, 8:38 PM
Matt Harrison
Matt Harrison - avatar
- 1
Why after except keyword divide zero error is written?Is it mandatory?
28th Nov 2020, 12:57 PM
TIRUMALARAJU HARI KRISHNA PRIYA
TIRUMALARAJU HARI KRISHNA PRIYA - avatar
- 1
Thanks mate
3rd Dec 2020, 12:27 PM
Furkan Coşkun
Furkan Coşkun - avatar