python - Error when trying to overload an operator "/" -


I have recently started gaming programming. Someone advises me to start with Python and gives me the book "Beginners With game development, Python and Pygam: From novice to professional "got me got a part where they used to teach vectors and make a vector 2 class. Everything was going well until I tried to surrender the running operator. My code is like this:

  square vector 2 (object): def __init __ (self, x = 0.0, y = 0.0): self.x = x self.y = y def __str __ (self): Return cells (P2, 0) - P1 [0], P2 [1] - P1 [1]) def __add __ (self, rhs): return vector 2 (self x + Rhs.x, itself .y + rhs.y) def __sub __ (auto, rhs): return vector 2 (self.x - rhs) .x, itself. Y - rhs.y) def __mul __ (self, scalar): return vector 2 (self x * scaler, self.) * Scaler def __div __ (self, scalar): return vector 2 (auto x / scalar) Auto / Scalar)  

Now, when I tried to call the "/" operator, it shows:

  AB = Vector 2 (10.0 , 25.0) Print (AB) # & lt; & Lt; & Lt; & Lt; (10.0, 25.0) v1 = ab + vector 2 (20, 10.) print (v1) # & lt; & Lt; & Lt; & Lt; (30.0, 35.0) V2 = AB - vector 2 (20., 10) print (v2) # and lieutenant; & Lt; & Lt; & Lt; (- 10.0, 15.0) v3 = ab * 3 print (v3) # & lt; & Lt; & Lt; & Lt; (30.0, 75.0) Print (v3 / 3) Type Error: Unsupported operation It was in Python 3.3, but if I run it with Python 2.7, everything works correctly Ype (s) for I / O : 'Vector2' and 'int'  

Where's the problem?

In Python 3.x, you must __flordive____ and __ Truediv __ operators, not __ div __ operator. Exits the pre // operation (returns an integer) and later / (returns a float).


Comments