I have to get pthagaros to get the distance between 2 points. so the function distanceBetweenPoints(Point(1, 2), Point(4, 6)) should result in 5.0. the parameters are supposed to be P1, P2, which i think i have done?. The get an error when i try to execute this?.. help please

def distanceBetweenPoints(p1, p2):
return float(math.sqrt((p2-p1)**2+(p2-p1)**2))

What error are you getting? It seems to run fine for me.

>>> import math
>>> def distance(p1, p2):
...    return float(math.sqrt((p2-p1)**2+(p2-p1)**2))
... 
>>> print distance(1,6)
7.07106781187
>>>

The get an error when i try to execute this?

Not enough info for anyone to help (could be an indentation error, or could be in the calcs). First add some print statements for the individual calcs to isolate the error, and post the specific error message.

def distance_between_points(p1, p2):
   p2subp1 = p2-p1
   p1subp2 = p1-p2
   print "after subtraction", p2subp1, p1subp2
   ## one print for each step

point_1 = (1, 2)
point_2 = (4, 6)
distance_between_points(point_1, point_2)

..

but the thing is we need it in this form.. distanceBetweenPoints(Point(1, 2), Point(4, 6)), so when we call the function and type it in we get the answer...

According to your previous post, I think that you are using the "graphics.py" module like this one. In that case, since p1 and p2 are not numbers but Points, you must use first p1.getX() and p1.getY() to get the coordinates of the points.

yeah but how do you do that?.. any examples you got?

For example

def myfunction(pointA, pointB):
    xa, ya = pointA.getX(), pointA.getY()
    xb, yb = pointB.getX(), pointB.getY()
    do_something_with(xa, ya, xb, yb)

anything around these lines?

def distanceBetweenPoints(p1, p2):
x1, y1 = P1.getX(), P1.getY()
x2, y2 = P2.getX(), P2.getY()
distance = float(math.sqrt((x2-x1)**2+(y2-y1)**2))

anything around these lines?

def distanceBetweenPoints(p1, p2):
x1, y1 = P1.getX(), P1.getY()
x2, y2 = P2.getX(), P2.getY()
distance = float(math.sqrt((x2-x1)**2+(y2-y1)**2))

The parameters p1 and p2 should not become P1 ad P2 the next line. Also, what does pyhon say ?

The parameters p1 and p2 should not become P1 ad P2 the next line. Also, what does pyhon say ?

def distanceBetweenPoints(p1, p2):
x1, y1 = p1.getX(), p1.getY()
x2, y2 = p2.getX(), p2.getY()
distanceBetweenPoints=float(math.sqrt((x2-x1)**2+(y2-y1)**2))

what about nw?.. python gives an error says "the name Point is not defined"

what about nw?.. python gives an error says "the name Point is not defined"

add

from graphics import *

at the top of your file. Also please use code tags to post your python code. And also post python error tracebacks if there is one.

add

from graphics import *

at the top of your file. Also please use code tags to post your python code. And also post python error tracebacks if there is one.

This is my code at the moment and when i type in distanceBetweenPoints(Point(1, 2), Point(4, 6))... there is no output in shell..

from graphics import *
import math
def distanceBetweenPoints(p1, p2):
    x1, y1 = p1.getX(), p1.getY()
    x2, y2 = p2.getX(), p2.getY()
    distanceBetweenPoints=float(math.sqrt((x2-x1)**2+(y2-y1)**2))

you forgot the return statement. This is python, not pascal :)

from graphics import *
import math
def distanceBetweenPoints(p1, p2):
    x1, y1 = p1.getX(), p1.getY()
    x2, y2 = p2.getX(), p2.getY()
    return math.sqrt((x2-x1)**2+(y2-y1)**2)

When you post a question like that, give as much pertinent information as you can. You needed to tell us right from the start that you are using the Zelle module graphics. Also, read the code that folks respond with and check it against the errors in your code.

I have to give Gribouillis and A+ for patience! Try this code, it seems to work now ...

from graphics import *
import math

def distanceBetweenPoints(p1, p2):
    x1, y1 = p1.getX(), p1.getY()
    x2, y2 = p2.getX(), p2.getY()
    return math.sqrt( (x2-x1)**2 + (y2-y1)**2 )

p1 = Point(1, 2)
p2 = Point(4, 6)
print( distanceBetweenPoints(p1, p2) )  # --> 5.0

yup.. thankyou so much "Gribouillis"... it wrks... cheers..

When you post a question like that, give as much pertinent information as you can. You needed to tell us right from the start that you are using the Zelle module graphics. Also, read the code that folks respond with and check it against the errors in your code.

I have to give Gribouillis and A+ for patience! Try this code, it seems to work now ...

from graphics import *
import math

def distanceBetweenPoints(p1, p2):
    x1, y1 = p1.getX(), p1.getY()
    x2, y2 = p2.getX(), p2.getY()
    return math.sqrt( (x2-x1)**2 + (y2-y1)**2 )

p1 = Point(1, 2)
p2 = Point(4, 6)
print( distanceBetweenPoints(p1, p2) )  # --> 5.0

ok will make sure.. i do everything possible frm next time.. you learn frm your mistakes right?.. ;)... thx for the help guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.