简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM