Identifying a complex number
>>> isinstance(1j, complex)
True
Try this:
if isinstance(x, complex):
print("Error 05: Complex Root")
This prints error for 2 + 0j
, 3j
, but does not print anything for 2
, 2.12
etc.
Also think about throwing an error (ValueError
or TypeError
) when the variable is complex.
I'm not 100% sure what you're asking, but if you want to check if a variable is of complex type you can use isinstance. For example,
x = 5j
if isinstance(x, complex):
print 'X is complex'
prints
X is complex