Syntax error not understood - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Syntax error not understood (/thread-1438.html) |
Syntax error not understood - raady07 - Jan-03-2017 Hi, Completely new user for Python coding. Please help to understand the error. import math def euclideanDistance(instance1,instance2, length): distance = 0 for x in range(length): distance += pow((instance1[x]-instance2[x],2) return math.sqrt(distance)it displays the following error
please ignore the thread it was just a syntax error distance += pow((instance1[x]-instance2[x],2)) RE: Syntax error not understood - Mekire - Jan-03-2017 When you get a syntax error like this the problem is often in the previous line. You forgot a parenthesis. RE: Syntax error not understood - Ofnuts - Jan-03-2017 Also, for integer powers, you can use the '**' operator: distance += (instance1[x]-instance2[x])**2 RE: Syntax error not understood - pedros - Jan-03-2017 slightly off-topic but it seems the ** operator isn't limited to integers, for either exponent or mantissa, which would allow one to change the code to not import math and simply raise the distance to the power of 1/2 >>> 9**0.5 3.0 RE: Syntax error not understood - Ofnuts - Jan-04-2017 (Jan-03-2017, 12:57 PM)pedros Wrote: slightly off-topic but it seems the ** operator isn't limited to integers, for either exponent or mantissa, which would allow one to change the code to not import math and simply raise the distance to the power of 1/2 math.sqrt() may have some optimizations... and a good compiler could replace the operation with inlined multiplications when the exponent of '**' is a small integer constant. RE: Syntax error not understood - Larz60+ - Jan-04-2017 One thing that was not mentioned. Your return statement is part of the loop. That means you will never see a second iteration, thus negating the purpose of the loop |