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))

Dani AI

Generated

Quick take: the symptom came from treating Point objects like plain numbers and from a couple of small Python mistakes (missing return, and accidentally reassigning the function name). correctly pointed out you must use a point’s coordinates; ’s advice to print intermediate values and post tracebacks is exactly the right debugging approach; demonstrated the expected result. Below is a compact, more defensive helper you can drop into your code that handles both Zelle-style Point objects and simple (x,y) pairs, and uses a numerically robust call to compute the Euclidean distance.

import math

def distance_between(a, b):
    """Accepts a Zelle Point (getX/getY) or an (x, y) sequence; returns float distance."""
    def _coords(p):
        try:
            return p.getX(), p.getY()
        except AttributeError:
            return p[0], p[1]

    x1, y1 = _coords(a)
    x2, y2 = _coords(b)
    return math.hypot(x2 - x1, y2 - y1)

Example usage: with Zelle Points or raw tuples/ lists the function returns the Euclidean distance (e.g., 5.0 for points (0,0) and (3,4)).

Troubleshooting notes and small gotchas:

  • "NameError: Point is not defined" usually means you didn’t import the graphics module or the file isn’t on Python’s path—use an explicit import (for clarity) rather than relying on wildcard imports.
  • Don’t assign a local variable the same name as your function (one poster did this and obscured the function).
  • Always include a return; forgetting it produces None and “no output.”
  • For debugging, print intermediate dx/dy or paste the full traceback when asking for help; that makes fixing indentation, typos, or attribute errors trivial.
  • If you’ll compute many distances, consider numpy vectorization for performance.

This keeps the solution flexible for different input styles and avoids the common pitfalls that came up earlier in the thread.

Recommended Answers

All 16 Replies

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 "" module . 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.