首页 文章

AttributeError:'module'对象没有属性'div'

提问于
浏览
4

我尝试运行以下使用python 3.2的程序,有错误:'module'对象没有属性'div'任何人都可以告诉我该怎么做才能解决这个问题?对此,我真的非常感激 !

import operator 
ops = {'+':operator.add,'-':operator.sub,'*':operator.mul,'/':operator.div}

AttributeError:'module'对象没有属性'div

2 回答

  • 2

    According to the docs,Python 3中有一个truediv和一个floordiv . 你需要使用其中一个 .

    operator.truediv(a,b)运算符.__ truediv (a,b)返回a / b,其中2/3是.66而不是0.这也称为“true”除法 . operator.floordiv(a,b)运算符. floordiv __(a,b)返回a // b

  • 5

    在Python 3 operator 模块中,您需要使用 truedivfloordiv . See the docs关于将映射运算符映射到运算符模块中的函数 .

相关问题