FAQ: Distance Formula - Euclidean Distance

This community-built FAQ covers the “Euclidean Distance” exercise from the lesson “Distance Formula”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

Machine Learning

FAQs on the exercise Euclidean Distance

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

For the sake of others, we’re going to keep your post up, even though by now you may well have figured out the answer on your own.

Euclidean_distance

In particular, note the qn, pn

Okay, so I obviously need alot of background knowledge to do this knowledge path. I had literally no idea how to write what the instructions told. Any tips on where to start?

Why is my code wrong? Ive been using java lately so im a little python rusty. I am asking specifically for the last line print(euclidean_distance(pt1,pt2)) as its not using my values for pt1 and pt2

pt1 = [1,2]
pt2 = [4,0]

def euclidean_distance (pt1,pt2) :
distance = 0

for i in range(len(pt1)):
distance += (pt1[i]-pt2[i]) **2

return (distance ** 0.5)
return distance

print (euclidean_distance(pt1,pt2))

I had to infer your indentation, but once I did that and commented out the odd second return statement, I got the expected answer. Did you resolve this on your own?

Anyone can refresh me on the concept of the for loop. I still quite don’t understand why we use range and len of pt1 only.

Because all the points have the same number of values, an x and y that make up a single coordinate.

A for loop implements an iterator on the object following the in keyword. When the object is a range, it iterates over that range. This is usually for the purpose for generating an index sequence, 0..len(object)-1 for use in a subscript to access the value at that index.

When the object following the in keyword is an iterable object, for iterates over the values in that object.

Thanks Roy, I figure it out the logic doing some exercices after

1 Like

Getting a progression error on this exercise on step 2. Even using the solution code verbatim and commenting out the lines that shouldn’t be progressed to yet (lines 5 thru EOF), it throws the following error and prevents progress:

unsupported operand type(s) for -: ‘NoneType’ and ‘float’

It works if you add the return statement on line 5, but that is not prompted by the exercise until the following step.

Anyways, moving on down the Machine learning path, but figured I’d flag this.

UPDATE: Figured it out. Step 1 lets you move on even if you haven’t put in “return distance” as instructed, but Step 2 won’t let you move on without it. The step validation check may need to be updated to include this.

Can someone please guide me with how to do step 2 of this exercise?
When I make the ‘for’ loop I don’t know where to put it.

I always get “SyntaxError: ‘return’ outside function.”

I am very new any help or advice would be appreciated :slight_smile:

Are you well informed as to what ‘Euclidean Distance’ is? It’s kind of important.

1 Like

I am not, haha.
I went back to take the beginning Python course and I think that will make everything in this exercise go much more smoothly.
Thank you for your reply! :slight_smile:

Brush up on Pythagoras and the Distance Formula as applied to a coordinate plane and mess around with the code on that concept. It grows from there.

FAQ: Distance Formula - Euclidean Distance - #3 by mtf

CC will not attempt to teach mathematics. It is the application of program logic to maths. We need our own wherewithal which means brushing up so we can dictate the objective.

1 Like

Just a suggestion:

for i,j in zip(pt1,pt2):

might be a nicer way to loop through two lists at once without having to worry about range and indices.

I am so confused about why my code is not working. I even tried copying the solution and I keep getting the same result.

I used this

def euclidean_distance(pt1, pt2):
  distance = 0
  for i in range(len(pt1)):
    distance = distance + (pt1[i] - pt2[i]) ** 2
    return distance

But i keep getting an error saying that my function is returning 9 instead of the expected 18. I don’t know what line is causing it to do 9 instead of 18.

I originally had this longer version of code, but ran into the same error:

def euclidean_distance(pt1, pt2) :
  distance=0
  subtracted= list()
  for x,y in zip(pt1,pt2):
    difference= (x-y)**2
    subtracted.append(difference)
    print(subtracted)
    distance=0
      for x in subtracted:
     distance= distance+x
     return distance

trying to change the indentations, but can’t seem to solve this!

That line should not be inside the loop. Unindent by two spaces so the loop gets to run all the way through.

1 Like