views:

286

answers:

6

Here's a small sample section of some code I'm writing with python and pygame, for some reason it seems to be claiming that some seemingly very simple and apparently accurate things are syntax errors. Here's possibly the most annoying example.

def Draw():
    Surface.fill((255,255,255))
    for square in squares:
        pygame.draw.rect(Surface,(0,0,0),(square.x,square.y,height,height)

def main():
    while True:
        GetInput()
        Move()
        CollisionDetect()
        Draw()

for some reason the word def at the start of line 6 get higlighted red and marked as an error, any idea why would be incredibly helpful, thanks :) (any other code you need, just comment).

+14  A: 

Unbalanced parentheses on line 4.

You're missing a closing )

Kena
wooooooooooo, 3rd time I've done that in 2 days, I so need to get some sleep... thanks though you guys :):):)
What kind of editor are you using? Any decent editor should be able to highlight mismatched braces and parentheses
Kena
I got gotten with this using Pydev. It has matching parentheses, but I couldn't see that it wasn't matched properly
Casebash
Why are there so many duplicate answers?
Casebash
+6  A: 

You missed one closing paranthesis:

pygame.draw.rect(Surface,(0,0,0),(square.x,square.y,height,height)

There are three "(" parenthesis but only two ")"

froh42
+6  A: 

It looks like you're missing a parenthesis on this line: pygame.draw.rect(Surface,(0,0,0),(square.x,square.y,height,height)

edit: I would like to add that in the future it might help if you paste whatever syntax error the interpreter gave you in your question.

2-bits
+3  A: 

Say, did anybody mention that you're missing a parenthesis? ;)

Cal Jacobson
+9  A: 

Here's a quick tip for dealing with syntax errors: if the compiler or interpreter is complaining about a completely normal looking line, check the line just above it.

David Locke
Absolutely! Works for almost all languages...
mtruesdell
You've piqued my curiosity. What language doesn't it work on?
David Locke
+3  A: 

Syntax errors in most languages happen AFTER the actual error, because the parser doesn't always KNOW something is wrong until it finds some later bit that can't be there. The parser complains about the thing that can't be, rather than what you did wrong.

tl;dr: Always look for the syntax error from where the compiler complained and work backwards toward the start of the file.

TokenMacGuy