2016-09-03 38 views
-3

我想製作一個程序來查找兩個座標之間的距離。有人能幫助我,我哪裏出錯了嗎?Python 3.5.2調用函數時出錯?

代碼:

from math import * 
prompt= input('Enter the coordinates \n') 
x= input() 
x1= input() 
y= input() 
y1= input() 
def dist(x,y,x1,y1): 
    dx=x1-x 
    dy=y1-y 
    return sqrt(dx**2 + dy**2) 
print ('The distance/Radius is: \n') 
dist(x,y,x1,y1) 
+1

大概你被告知它不能減去字符串......想知道爲什麼會這樣? – jonrsharpe

回答

3

我的解決方案:

x= int(input()) 
x1= int(input()) 
y= int(input()) 
y1= int(input()) 
def dist(x,y,x1,y1): 
    dx=x1-x 
    dy=y1-y 
    return ((dx**2 + dy**2)**(0.5)) 
print('The distance/Radius is:') 
print(dist(x,y,x1,y1)) 

輸入:

1 
2 
2 
3 

輸出:

The distance/Radius is: 
1.4142135623730951 

在執行計算之前,您必須將input()轉換爲integer。而對於在這裏執行平方根,我做數學計算爲sqrt= x**(.5)

+0

這將有助於添加一段說明你已經改變了什麼,以及爲什麼 – jonrsharpe

+0

感謝您的幫助:) – Vito

+0

@jonrsharpe謝謝,我添加了解釋 –