4

I've been trying to calculate this expression for a field in an attribute table in ArcMap 10.4.1, and keep getting the following error:

Error 000539: SyntaxError: invalid syntax (expression, line 5)

60 *math.degrees((math.acos(math.sin( math.radians(!LAT_O! ))
*math.sin(  math.radians(!LAT_DEST!) ) 
+ math.cos( math.radians( !LAT_O!) ) 
*math.cos( math.radians( !LAT_DEST! ))  
*math.cos( math.radians(!Dist_Long! )))

They are all number fields - Any idea where I'm going wrong?

2
  • Linting your code is a good habit to get into. It catches silly syntax errors that can cost you valuable time searching for. pylint.org
    – RomaH
    Nov 15, 2016 at 2:49
  • @RomaH While I would normally agree with that, ESRI's field calculator syntax isn't standard Python. The primary difference is the !COLUMN! syntax, which is most certainly non-standard Python and doesn't pass pylint despite being correct in ESRI's field calculator. If the OP is actually writing a module instead, you're correct, of course.
    – jpmc26
    Nov 15, 2016 at 16:55

2 Answers 2

4

You are missing two )) at the end

3

You're missing closing parentheses. For complex expressions, you really need to leverage whitespace to help you catch this stuff. Try some indentation:

60 * math.degrees((
    math.acos(
        math.sin(math.radians( !LAT_O! )) *
        math.sin(math.radians( !LAT_DEST! )) +
        math.cos(math.radians( !LAT_O! )) *
        math.cos(math.radians( !LAT_DEST! )) *
        math.cos(math.radians( !Dist_Long! ))
    )
)) # These two are missing in yours!

I've chosen to format this by making a set of parentheses open and close on the same line or having the closing parenthesis line up vertically with the line of the opening parenthesis.

This makes the missing closing parentheses at the end much more obvious, since you can just look vertically down and see there's nothing lining up with the first line. Note that this also reveals that you don't even need the second parenthesis after math.degrees.

I might even go a little further, and add a couple extra parentheses to make it more clear what I'm multiplying and what I'm adding together:

60 * math.degrees((
    math.acos(
        math.sin(math.radians( !LAT_O! )) * math.sin(math.radians( !LAT_DEST! )) +
        (
            math.cos(math.radians( !LAT_O! )) *
            math.cos(math.radians( !LAT_DEST! )) *
            math.cos(math.radians( !Dist_Long! ))
        )
    )
)) # Missing in yours!

Using indentation well can help you catch syntax and logic errors. It makes it much easier to visually scan your code and identify details about the structure of it.

Settle on a standard and stick to it. It's okay to decide what you chose isn't working out for you and switch to something else, of course. And it's also okay to violate your standard when your standard actually makes it less readable. (If that happens a lot, you should think about revising your standard.) See PEP 8 for some standard approaches to formatting in Python.

1
  • Thank you very much for the advice - I'll try and follow this for future work!
    – ElenaFF
    Nov 15, 2016 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.