Level: Beginner
In the following code my 'samePoint' function returns False where i am expecting True. Any hints?
import math
class cPoint:
def __init__(self,x,y):
self.x = x
self.y = y
self.radius = math.sqrt(self.x*self.x + self.y*self.y)
self.angle = math.atan2(self.y,self.x)
def cartesian(self):
return (self.x, self.y)
def polar(self):
return (self.radius, self.angle)
class pPoint:
def __init__(self,r,a):
self.radius = r
self.angle = a
self.x = r * math.cos(a)
self.y = r * math.sin(a)
def cartesian(self):
return (self.x, self.y)
def polar(self):
return (self.radius, self.angle)
def samePoint(p, q):
return (p.cartesian == q.cartesian)
>>> p = cPoint(1.0000000000000002, 2.0)
>>> q = pPoint(2.23606797749979, 1.1071487177940904)
>>> p.cartesian()
(1.0000000000000002, 2.0)
>>> q.cartesian()
(1.0000000000000002, 2.0)
>>> samePoint(p, q)
False
>>>
source: MIT OpenCourseWare http://ocw.mit.edu Introduction to Computer Science and Programming Fall 2008
Looking at your code
def samePoint(p, q):
return (p.cartesian == q.cartesian)
p.cartesian, q.cartesian are functions and you are comparing function rather than function result. Since the comparing two distinct functions, the result is False
What you should have been coding is
def samePoint(p, q):
return (p.cartesian() == q.cartesian())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With