Python: what is the wrong in this code

Thread Starter

Bamerni

Joined Jun 26, 2016
53
Hello everyone

I have the following code that calculate the distance between two points x(0,1) and y(0,1) but I don't get the answer

Python:
def distance(x, y): 
    dist = math.sqrt((x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2 )
    return dist
    
print(distance((0,0),(1,1)))
Please can anyone help me correct this error?

thank you in advance
 

upand_at_them

Joined May 15, 2010
940
If that's your entire program you're missing the "import math" at the top. Aside from that I don't know what you're expecting that isn't working.
 

daGrizz

Joined Dec 22, 2020
1
Hello everyone

I have the following code that calculate the distance between two points x(0,1) and y(0,1) but I don't get the answer

Python:
def distance(x, y):
    dist = math.sqrt((x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2 )
    return dist
   
print(distance((0,0),(1,1)))
Please can anyone help me correct this error?

thank you in advance
Just in case you're still waiting for an answer...
When you use 'Sum of Squares' to compute distance, you need to use the (difference in X) ** 2 + (difference in Y) ** 2, so ...
dist = math.sqrt((x[1] - x[0]) ** 2 + (y[1] - y[0]) ** 2 )

Best o' luck to ya.
 
Top