2017-11-02 7 views
1

をとり、プログラムは後で距離ポイントの計算で失敗:例外TypeError:距離は()私は、ユーザの入力を提供する際class.But指すようにパラメータを渡すことによって、距離をチェックしようとしています正確に2つの引数(1は、与えられた)

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) 

私は、ファイルを実行する上で、次のエラーを取得:

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

私は、このエラーurgently.Anyのヘルプを取り除くために必要がいただければ幸いです。

答えて

1

変更:

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

に:

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

説明:distanceは、クラスメソッド(静的メソッド)ではない、したがって、それは、クラスのオブジェクトに対して呼び出されるべきである - ないでクラスそのもの。したがって、最初の呼び出しPoint(x1,y1)は、変数(ここでは私はxを使用しました)に割り当てられ、次に作成されたこのPointオブジェクトを使用して、オンザフライで作成された他の点からの距離を測定します:Point(x2,y2)

我々はまた、他の点作成して保存できます。

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

距離のパラメータとしてselfがあります。したがって、コールはPoint.distance(x1, y2)ではなく、point1.distance(point2)

また、Point(x1、y1)は実際に何もしません。あなたはそれをどこかに割り当てる必要があります。等point1 = Point(x1, y1)

関連する問題