2024-05-20 23:44:40 发布
网友
我怎么写“如果数字是自然返回真的”?我
我试过使用int和float命令,但是没有用
def perfectsquareroot(a): import math x=math.sqrt(a) if x is int return True else return False
如果平方根的结果不是自然数,我希望返回false
def perfectsquareroot(a): import math x=math.sqrt(a) return x == int(x)
Syntax of math.sqrt() method: math.sqrt(n) Parameter(s): n – a number whose square root needs to be calculated.Return value: float – it returns a float value that is the square root of given number n.
Syntax of math.sqrt() method:
math.sqrt(n)
Parameter(s): n – a number whose square root needs to be calculated.
Return value: float – it returns a float value that is the square root of given number n.
你知道吗数学.sqrt返回的是浮点数,这意味着不能只检查结果是否为整数,因为它永远不会是整数。你知道吗
import math def perfectsquareroot(a): x = math.sqrt(a) if x%1 == 0 and x >= 0 : return True else: return False print (perfectsquareroot(4))
输出:
True
或:
import math def perfectsquareroot(a): x = math.sqrt(a) if int(x) == x and x >= 0: return True else: return False print (perfectsquareroot(4))
是的
你知道吗数学.sqrt返回的是浮点数,这意味着不能只检查结果是否为整数,因为它永远不会是整数。你知道吗
输出:
或:
输出:
是的
相关问题 更多 >
编程相关推荐