Objective
Create a Python program with a function that calculates the square root and raises an exception when the number is negative.
Instructions
- Define the function
square_root(x): ifxis negative, raiseValueError("Negative number")withraise; otherwise, returnmath.sqrt(x). - Read a decimal number and call the function inside a
tryblock. - If all goes well, show the root with two decimals.
- If the exception is raised, show
Error:followed by the exception message.
16
4.00
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise