Python3.7
真的搞不懂 我的for循环里面连个除法都不涉及,怎么就存在浮点数了
提示错误内容如下:
File "C:/Users/stars/Desktop/temp.py", line 34, in aisprime
for i in range(2,abs(number)):
TypeError: 'float' object cannot be interpreted as an integer
我的for循环:
def isprime(number):
for i in range(2,number):
if number%i==0:
# print('no')
break
else:
# print('yes')
return 1
解决办法: 强行给number(这个它认为的浮点数?)加上int限制,如果直接变成int(number)是不行的哦 还得故意绕个弯给个计算再将结果变成int类型
def aisprime(number):
for i in range(2,int(np.sqrt(number))):
if number%i==0:
# print('no')
break
else:
# print('yes')
return 1
具体是为啥我还不得而知,等我知道了再补充?