Mar-04-2017, 08:44 AM
I would recommend to omit the 'else'.
Oh, one thing: you are sure you use python 3? Your print-statements look like being python 2?
try: result = x / y print("result is", result) except ZeroDivisionError: print("division by zero!")If you predefine a 'result', you can also do:
result = None x = 4 y = 0 try: result = x / y except ZeroDivisionError: print("division by zero!") print("result is", result)If you do:
x = 4 y = 0 try: print("x is ", x) print("y is", y) result = x / y print("result is", result) except ZeroDivisionError: print("division by zero!")then you will see that the both first printed lines appear. During execution, everything is still fine there. Python only 'leaps' to the except part for further execution if it finds something going wrong. Everyting that can be done without the 'output' of the try-statement will be done when execution reaches is.
Oh, one thing: you are sure you use python 3? Your print-statements look like being python 2?