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
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?
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.
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.
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!
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.
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!