1

I started learning python yesterday , and I realized I can make a perfect square checker using functions and the isinstance function. However , my code says 144 is not a perfect square. What am I doing wrong?

My Code :

def sqrt():

    x = int(input("Enter a number:"))
    a = x ** 0.5
    return a

b = sqrt()

if isinstance ( b , int) == True:

    print("It is a perfect square")
if isinstance( b , int) == False:

    print("It is not a perfect square")
D.L
  • 4,339
  • 5
  • 22
  • 45

2 Answers2

3

Keep in mind that number ** 0.5 will always result to a floating value. Example: 144 ** 0.5 == 12.0. Because of this, isinstance(b , int) will always be False.

This is an alternative solution among many other possible solutions:

def is_perfect_square(number):
    root = int(number ** 0.5)
    return root ** 2 == number

print(is_perfect_square(144))
Jobo Fernandez
  • 905
  • 4
  • 13
  • 1
    Alternatively a simple mod function can be used to check the difference between the float sqrt against the int sqrt. – Nizar Aug 13 '22 at 11:05
  • 2
    One could also simplify the returns to `return root ** 2 == number` – CrazyChucky Aug 13 '22 at 11:05
  • in the return True and return False parts of your code , are True and False strings? why didnt you use else before the return False part? – siddhantuniyal Aug 13 '22 at 11:17
  • @siddhantuniyal ```True``` and ```False``` are reserved keywords for boolean values. An else statement can also be added before the ```return False``` statement (this is just a shortcut applicable to certain cases). – Jobo Fernandez Aug 13 '22 at 11:24
  • 1
    @JoboFernandez - this will fail for rational numbers between 1 and 0, like 0.04. – Joe Carboni Aug 13 '22 at 16:51
-1

The type of b in the original code is a float.

you can see this by adding type(b) to the end of the code which would return <class 'float'>.

so you could change the code to this:

def sqrt():

    x = int(input("Enter a number:"))
    a = x ** 0.5
    return a

b = sqrt()

if b - int(b) == 0:
    print("It is a perfect square")

else:
    print("It is not a perfect square")

This method avoids checking for type, but instead checks if the decimal part == 0 which is sufficient for a square.

Note: there are other methods.

D.L
  • 4,339
  • 5
  • 22
  • 45
  • why is 12.0 - 12 = 0.0 not a float then? – siddhantuniyal Aug 13 '22 at 11:13
  • that is a great question which is why i put there are other methods, some will use the `math` library and other the `decimal` library. But that is a separate question... the OP asked what they are doing wrong... but asnswering, it doesnt matter because not looking for `type` with this method. just checking if the **decimal part == 0**. – D.L Aug 13 '22 at 11:19
  • Be careful! This could give incorrect answers because of [floating point imprecision](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). – CrazyChucky Aug 13 '22 at 14:56
  • @CrazyChucky agreed, one of the biggest issues in coding since its creation. – D.L Aug 13 '22 at 14:58