Intermediate Python

Creating a Custom Calculator

Your task is to create a Number class that utilizes Python's magic methods (__add__, __sub__, __mul__, __truediv__) to perform mathematical operations. However, the class needs to be robust and be able to handle various exceptions that can occur during these operations.
If a division by zero is attempted, a ZeroDivisionError should be raised.
If either of the inputs cannot be converted to a number, a ValueError should be raised.
If the operator is not a string, a TypeError should be raised.
Additionally, you should implement a custom exception, UnsupportedOperatorError that inherits from ValueError. This error should be raised if an operation is attempted that is not supported by the class. That includes __floordiv__, __mod__, __pow__, __lshift__, __rshift__, __and__, __xor__, __or__.
Input
Output
a=Number(10); b=Number(2); print(a+b); print(a/b);
12 5
a=Number(10); b=Number(0); print(a/b);
Cannot divide by zero
a=Number('ten'); b=Number(2); print(a+b);
Invalid number input
a=Number(10); b=Number(2); print(a**b);
Unsupported operator
a=Number(10); b=2; print(a+b);
Operator must be a Number
 

Constraints

Time limit: 0.2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue