【发布时间】:2011-01-19 16:44:28
【问题描述】:
我在这里遗漏了技术词,但这里的问题是将 int 更改为 float 或将 float 更改为 int。
def factorize(n):
def isPrime(n):
return not [x for x in range(2,int(math.sqrt(n)))
if n%x == 0]
primes = []
candidates = range(2,n+1)
candidate = 2
while not primes and candidate in candidates:
if n%candidate == 0 and isPrime(candidate):
# WHY ERROR?
#I have tried here to add float(), int() but cannot understand why it returns err
primes = primes + [float(candidate)] + float(factorize(n/candidate))
candidate += 1
return primes
错误 - 尝试使用 int() 和 float() 等函数修复它,但仍然存在:
TypeError: 'float' object cannot be interpreted as an integer
【问题讨论】:
-
factorize函数中为什么要包含浮点数? -
dan04:只是过度使用,杀了它。现在它可以工作了,但仍然想知道其他问题,更简洁?
标签: python floating-point int factorization