Search code examples
pythonloopsuser-inputperfect-square

Getting Multiple Inputs and checking if they are Perfect squares or not without using math


def isPerfectSquare(n) :
 
    i = 1
    while(i * i<= n):
        if ((n % i == 0) and (n / i == i)):
            return True
        i = i + 1
    return False

lst=[]
n=int(input())
for i in range(0,n):
  ele=int(input("Enter: "))
  lst.append(ele)
for i in lst:
  isPerfectSquare(i)
  if (isPerfectSquare(n)):
    print("Perf")
  else:
    print("Not")

I am a new python programmer so I am trying out different low levelled problems with a twist. I tried with for loops first but couldn't figure out how to do it with multiple inputs. Where did I go wrong? When I input a perfect square, it's not working.


Solution

  • Issue is if (isPerfectSquare(n)): you were always just passing in the input and checking if that is a Perfect Square. You should be passing in each element of the list instead like this if isPerfectSquare(i):

    def isPerfectSquare(n):
        i = 1
        while i * i <= n:
            if (n % i == 0) and (n / i == i):
                return True
            i = i + 1
        return False
    
    
    lst = []
    n = int(input())
    for i in range(0, n):
        ele = int(input("Enter: "))
        lst.append(ele)
    for i in lst:
        if isPerfectSquare(i):
            print("Perf")
        else:
            print("Not")
    

    Output:

    3
    Enter: 1
    Enter: 4
    Enter: 10
    Perf
    Perf
    Not