+ 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")
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).
+ 3
HonFu Thanks mate!
- 1
Why after except keyword divide zero error is written?Is it mandatory?
- 1
Thanks mate