-1

Using the variable 4 as an example, the result of 2.0 is clearly a float. I have no idea why it triggers my first if statement. Is it because square_root is a reference to the variable and not actually 2.0? I am expecting to take complex numbers and I need "is not isinstance" to work properly.

square_root = 4 ** (1/2)


if square_root != isinstance(square_root,float):
    return False
if square_root.is_integer():
    return True
else:
    return False
Dan Nand
  • 13
  • 3
  • 2
    What **type** does `isinstance` return? It is, pretty sure, not the **type** of `square_root ` so they won't be `==` – DeepSpace Mar 09 '21 at 19:48
  • 1
    What is `squared`? Please post a [mcve], one that doesn't depend on variables defined elsewhere (although, in this case, @DeepSpace has clearly identified the problem) – John Coleman Mar 09 '21 at 19:48
  • I fixed my post, I meant square_root instead of squared – Dan Nand Mar 09 '21 at 19:51
  • Does this answer your question? [What are the differences between type() and isinstance()?](https://stackoverflow.com/questions/1549801/what-are-the-differences-between-type-and-isinstance) – Elephant Mar 09 '21 at 20:55
  • Does this answer your question? [Best way to see if a number is fractional or not](https://stackoverflow.com/questions/53017876/best-way-to-see-if-a-number-is-fractional-or-not) – Peter O. Mar 09 '21 at 23:31

1 Answers1

1

isinstance already returns the boolean you are looking for, comparing it against square_root makes no sense at all and it will always be False because it compares a numerical value to a boolean.

Technically, if square_root != isinstance(square_root,float): should just be if isinstance(square_root,float):, but I'm not sure when exactly you expect this check to be False. 4 ** (1/2) (using any numbers) will always return a float.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154