My code is
我的代码是
if graph == square_grid and type(math.sqrt(nodes)) is not int:
print "Your netork can't have that number of nodes"
Of course this doesn't work because math.sqrt always returns a float. How can I do this?
当然这不起作用,因为math.sqrt总是返回一个浮点数。我怎样才能做到这一点?
3 个解决方案
#1
13
One way is
一种方法是
int(math.sqrt(x)) ** 2 == x
#2
7
Because math.sqrt always returns a float, you can use the built in is_integer
method
因为math.sqrt总是返回一个浮点数,所以可以使用内置的is_integer方法
def is_square(x):
answer = math.sqrt(x)
return answer.is_integer()
this will return True
if x
is a square and False
if it's not
如果x是正方形,则返回True,否则返回False
>>> is_square(25)
True
>>> is_square(14)
False
#3
1
try:
尝试:
math.sqrt(nodes) == int(math.sqrt(nodes))
#1
13
One way is
一种方法是
int(math.sqrt(x)) ** 2 == x
#2
7
Because math.sqrt always returns a float, you can use the built in is_integer
method
因为math.sqrt总是返回一个浮点数,所以可以使用内置的is_integer方法
def is_square(x):
answer = math.sqrt(x)
return answer.is_integer()
this will return True
if x
is a square and False
if it's not
如果x是正方形,则返回True,否则返回False
>>> is_square(25)
True
>>> is_square(14)
False
#3
1
try:
尝试:
math.sqrt(nodes) == int(math.sqrt(nodes))