2017-05-08 8 views
-1

私が抱えている問題は、ポイントMがポイントMよりも短くても、ポイントMよりも短くても、プログラムは同じ距離であることを印刷するだけです。これは私の戻り値に問題がありますか? if、elif、else文を使用していますか?には何もありません()私のプログラムは、if、elif、else文の戻り値を比較しないのはなぜですか?

import math 
print("This Program takes the coordinates of two points (Point M and N) and uses the distance formula to find which " 
     "point is closer to Point P (-1, 2).") 

x_P = -1 
y_P = 2 

def main(): 
    x_1 = int(input("Enter an x coordinate for the first point: ")) 
    y_1 = int(input("Enter an x coordinate for the first point: ")) 
    x_2 = int(input("Enter an x coordinate for the second point: ")) 
    y_2 = int(input("Enter an x coordinate for the second point: ")) 
    distance(x_1, y_1, x_2, y_2) 
    distance1 = 0 
    distance2 = 0 
    if distance1 < distance2: 
     print("Point M is closer to Point P.") 
    elif distance1 > distance2: 
     print("Point N is closer to Point P.") 
    else: 
     print("Points M and N are the same distance from Point P.") 


def distance(x_1, y_1, x_2, y_2): 
    distance1 = math.sqrt((x_P - x_1) ** 2 + (y_P - y_1) ** 2) 
    distance2 = math.sqrt((x_P - x_2) ** 2 + (y_P - y_2) ** 2) 
    return distance1, distance2 


main() 

答えて

0

はあなたのreturn値を使用する必要があるので、代わりに

distance(x_1, y_1, x_2, y_2) 
distance1 = 0 
distance2 = 0 

のあなたは距離に

distance1, distance2 = distance(x_1, y_1, x_2, y_2) 
+0

ありがとうございました!すべてが正しく機能するようにしました! – AndroidFan253

0
distance(x_1, y_1, x_2, y_2) 
distance1 = 0 
distance2 = 0 

あなたの呼び出しを使用する必要があります戻り値を適用するので、本質的に消えます。 distance()関数のdistance1とdistance2は、関数自体の名前空間に対してローカルです。そうでない場合でも、distance1 = 0ステートメントとdistance2 = 0ステートメントで上書きします。

簡単修正:

(distance1, distance2) = distance(x_1, y_1, x_2, y_2) 

あなたの関数はタプルを返すので、ちょうどメインの名前空間であなたの変数を持つタプルを受け入れます。

ああ、すべての入力はx値を求めています。

関連する問題