TypeError: 'float' object cannot be interpreted as an integer

【TypeError: 'float' object cannot be interpreted as an integer】
TypeError: 'float' object cannot be interpreted as an integer

    • 命令行报错
    • 源代码
    • 原因
    • 解决

命令行报错 在使用案例中的代码时,报错
TypeError: 'float' object cannot be interpreted as an integer

源代码
#!/usr/bin/python # -*- coding:utf-8 -*-import operatordef c(n, k): return reduce(operator.mul, range(n-k+1, n+1)) / reduce(operator.mul, range(1, k+1))def bagging(n, p): s = 0 for i in range(n / 2 + 1, n + 1): s += c(n, i) * p ** i * (1 - p) ** (n - i) return sif __name__ == "__main__": for t in range(10, 101, 10): print (t, '次采样正确率:', bagging(t, 0.6))

原因 案例中是用的python2,/ 是取整,是int型
而python3,/ 是保留小数的,是float型
解决 // 即可

    推荐阅读