2016-08-29 10 views
-1

私のコードでは、すべての座標の組み合わせを試して、指定した円の中にいくつの点があるのか​​を数えることができます。 私は私のコードを実行するたびに、私はここでValueError: math domain errorPython 3で数字を四角形にすることはできません

取得すること

def findPoints(radius): 
x = 0 
y = 0 
centre_x = 0 
centre_y = 0 
numOfPoints = 0 
allFound = False 
while allFound == False: 
    print(str(numOfPoints) + " :::: " + str(x) + "::" + str(y)) 
    dist = math.sqrt(centre_x - x)**2 + (centre_y - y)**2 
    if dist < radius: 
     numOfPoints = numOfPoints + 1 
     x = x + 1 
     if x == radius: 
      x = 0 
      y = y + 1 
+1

私は、あなたが決して 'centre_x'を増やさないので、あなたは負の数のsqrtを取ろうとします。 – Karin

答えて

4
dist = math.sqrt(centre_x - x)**2 + (centre_y - y)**2 

あなたは括弧のセットを逃している私のコードです...これは次のようになります。

dist = math.sqrt((centre_x - x)**2 + (centre_y - y)**2) 
0

この行を見てください:

dist = math.sqrt(centre_x - x)**2 + (centre_y - y)**2 

括弧の使用を確認してください。問題を解決できると思います。

+1

これは質問に答えない、それはコメントでなければならない。 – Barmar

関連する問題