Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax error not understood
#1
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 

Error:
File "test6.py", line 9     return math.sqrt(distance)          ^ SyntaxError: invalid syntax

please ignore the thread it was just a syntax error

distance += pow((instance1[x]-instance2[x],2))
Reply
#2
When you get a syntax error like this the problem is often in the previous line.
You forgot a parenthesis.
Reply
#3
Also, for integer powers, you can use the '**' operator:
distance += (instance1[x]-instance2[x])**2 
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
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
Reply
#5
(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

>>> 9**0.5
3.0

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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation getting a syntax error and i don't know why ilikedofs 5 90 9 hours ago
Last Post: ilikedofs
  Even number code syntax error MrCeez 1 156 May-02-2021, 06:43 PM
Last Post: Larz60+
  error in your SQL syntax Xuli 10 252 May-02-2021, 12:27 PM
Last Post: Xuli
  f-string error message not understood Skaperen 4 377 Mar-16-2021, 07:59 PM
Last Post: Skaperen
  Function syntax error Changosoyyo 2 302 Mar-07-2021, 10:42 PM
Last Post: Changosoyyo
  Syntax error on line 29 manwithjeans 3 327 Feb-27-2021, 08:28 AM
Last Post: perfringo
  Invalid syntax error - need help fixing calgk01 3 378 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Why do I get Syntax error? TechNitium 1 288 Jan-22-2021, 12:49 PM
Last Post: snippsat
  syntax error CompleteNewb 8 565 Jan-08-2021, 04:05 PM
Last Post: deanhystad
  Syntax Error: Outside a function? aplusfordao2702 2 350 Dec-01-2020, 03:25 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020