Dkk
Dkk

Reputation: 63

TypeError: distance() takes exactly 2 arguments (1 given)

I am trying to check distance by passing parameters to point class.But when i provide the user input,program later fails at calculation of distance point:

import math
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def distance(self, point):
        return math.sqrt((self.x-point.x)**2+ (self.y-point.y)**2)

class Circle(Point):
    @classmethod
    def envelops(self, shape):
        if shape == "Circle":
            r1 = float(input("Enter radius first circle:"))
            r2 = float(input("Enter radius of second circle:"))
            x1 = float(input("Enter first circle's x coordinate: "))
            x2 = float(input("Enter second circle's x coordinate: "))
            y1 = float(input("Enter first circle's y coordinate: "))
            y2 = float(input("Enter second circle's y coordinate: "))
            Point(x1,y1)
            dist=(Point.distance(Point(x2,y2)))
            if r1 > (r2 + dist):
                print "First Circle envelops the second circle"
            else:
                pass



if __name__ == "__main__":
    shape = 'Circle'
    Circle.envelops(shape)

I get the following error on executing the file:

    dist=(Point.distance(Point(x2,y2)))
TypeError: distance() takes exactly 2 arguments (1 given)

I need to get rid of this error urgently.Any help would be appreciated.

Upvotes: 1

Views: 1474

Answers (2)

Nir Alfasi
Nir Alfasi

Reputation: 53525

Change:

Point(x1,y1)
dist=(Point.distance(Point(x2,y2)))

to:

x = Point(x1,y1)
dist = x.distance(Point(x2,y2))

Explanation: distance is not a class method (static method) hence it should be called on an object of the class - not on the class itself. So the first call Point(x1,y1) should be assigned to a variable (here I used x) and then we'll use this Point object that we just created to measure the distance from the other point which is created on-the-fly: Point(x2,y2).

We could also create and save the other point:

x = Point(x1,y1)
y = Point(x2,y2)
dist = x.distance(y)  # and now call it with both points

Upvotes: 1

user8502296
user8502296

Reputation:

You have self as a parameter for distance. So the call wouldn't be Point.distance(x1, y2) but point1.distance(point2)

Also Point(x1, y1) doesn't really do anything. You need to assign that somewhere. Like point1 = Point(x1, y1)

Upvotes: 0

Related Questions