编写一个名为 squares 的函数,它接受一个名为 n 的参数。如果 n 是一个正整数,该函数返回一个列表,该列表由 1 到 n 的数字的平方组成。否则(即,如果 n 不是正整数)该函数应该返回一个空列表。
即使使用无意义的参数调用该函数,该函数也能正常工作。换句话说,诸如 squares('Iowa')、squares(range(10)) 和 squares([1, 2, 3]) 之类的函数调用应该返回一个空列表。
def sqrt(x): ans = 0 if x >= 0: while ans*ans < x: ans+=1 if ans *ans == x:return ans;
return -1;
def square(n): i=0 blist= [] ans = list(range(1, n))
if (n < 0):
return blist
if isinstance(ans, str):
return blist
while( i < len(ans)):
if(sqrt(ans[i])!= -1):
blist.append(ans[i])
i = i+1
return blist
i = square('男孩') 打印(i)