AttributeError: object 'module' does not have attribute 'div'

I tried to run the following program using python 3.2, there is an error: the "module" object does not have the "div" attribute. Can anyone tell me what should I do to fix this? I really appreciate it!

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

AttributeError: object 'module' does not have attribute 'div

+4
source share
2 answers

According to docs , in Python 3 there are truediv and floordiv. You need to use one of them.

operator.truediv (a, b) operator .__ truediv __ (a, b) Returns a / b, where 2 / 3.66, not 0. This is also known as the "true" division.

Operator

operator.floordiv(a, b).__ floordiv __ (a, b) a//b

+5

Python 3 operator truediv, floordiv. . .

+2

All Articles