else and finally
Completed
Read a number. In a try convert it to float. In except print bad input; in else print its square root (2 decimals, negative numbers give negative); in finally print done.
Input
One line.
Output
Two lines.
Example
Input
16
Output
4.00 done
Editor
Python
Hints
else runs only if no exception happened.
finally always runs.
Solution explanation
Keep the try block small; put follow-up work in else and cleanup in finally.
Show solution
import math
text = input()
try:
x = float(text)
except ValueError:
print("bad input")
else:
print(f"{math.sqrt(x):.2f}" if x >= 0 else "negative")
finally:
print("done")