type函数返回的不是数据的类型吗,为什么不能参与运算
import math
a=1
while 1==1:
if type(math.sqrt(a+100))==class 'int' and type(math.sqrt(a+168))==class 'int' :
print(a)
break
type方法返回的数据类型是一个type对象,type对象不能直接用==比较是否相等,你可以转为str字符串后再判断是否相等。其次math.sqrt的输出结果是float类型的。代码修改如下,请采纳哦
import math
a = 1
while 1 == 1:
print(type(math.sqrt(a + 100)))
if str(type(math.sqrt(a + 100))) == "<class 'float'>" and str(type(math.sqrt(a+168))) == "<class 'float'>":
print(a)
break
type返回的不是字符串,不能这样比较
一般都是用 isinstance 来判断是否为某一类,另外开平方是得不到整数的,必然是浮点数,更何况101和169的平方根,所以你这是个死循环。
修改如下:
import math
a=1
while 1==1:
if isinstance(math.sqrt(a+100), float) and isinstance(math.sqrt(a+168), float) :
print(a)
break
在 Python 中,type 函数返回的是一个类型对象,而不是类型本身。因此,你需要将返回值与要比较的类型进行比较,而不是直接将类型名称写在 type 函数的返回值后面。
import math
a = 1
while True:
if isinstance(math.sqrt(a + 100), int) and isinstance(math.sqrt(a + 168), int):
print(a)
break
a += 1
if age >= 18:
print(“可以进网吧嗨皮……”)