Came across this gem today:
def reciprocal(n): try: n = 1 / n except ZeroDivisionError: print("Division failed") n = None else: print("Everything went fine") finally: print("It's time to say goodbye") return n print(reciprocal(2)) print(reciprocal(0))This made is tidier, but can't the content of the else block go into the try block and the finally block outside the try statement entirely - as below:
def reciprocal(n): try: n = 1 / n print("Everything went fine") except ZeroDivisionError: print("Division failed") n = None print("It's time to say goodbye") return nDo they else and finally blocks serve a better purpose then then a "cleaner" statement?
buran write Jan-12-2021, 03:46 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.