Syntax error

Do you need to test it?

If you’ve found something that isn’t ok, adjust it and run it again.

Deleting would be if you have no clue where, as a way of locating a problem because when you delete a problem it stops being reported and therefore tells you the location.

Have you ever looked up a name in a phone book? They’re pretty thick, but it doesn’t take very long to find a specific name. Why is that? Because you can tell if your guess is too high or too low.

I see.
well I guess I better pay more attention to what I type
thanks for the help.

the trick isn’t to not make mistakes. you will.
the trick is to know how to locate it.

and it may even be slow to do so at first.
but when you stop guessing or trying to stare it down, and instead employ a method that will result in you finding it, that’s when these things become trivial

1 Like

Hi @temimam,

This program may look strange, but its syntax is valid, and it executes without any problem:

fahrenheit = 212
celsius = (fahrenheit - 32.0 # line 2

           ) / 1.8           # line 4

print(celsius)               # line 6

It is no problem that the opening parenthesis on line 2 is not matched on that same line, since it gets matched with a closing one on line 4.

In the following, the closing parenthesis has been removed from line 4, and the program raises a SyntaxError:

fahrenheit = 212
celsius = (fahrenheit - 32.0 # line 2

           / 1.8           # line 4

print(celsius)               # line 6

Here’s the message:

  File "", line 6
    print(celsius)               # line 6
SyntaxError: invalid syntax

Note that the problem is described as occurring on line 6. Do you know why that happens?

1 Like

because the program is looking for the closing parenthesis, and is getting something else instead. so it is saying that the code on line 6 doesn’t match the expected.

Closing parenthesis is one of several things that the parser is willing to accept in that state, otherwise you would for example not be allowed to do this: (3, 4)

Currently it would accept things that build on the previous expression to make a bigger expression (like an arithmetic operator followed by another value), or something like a comma … there does eventually have to come a closing parenthesis though, since the parser is currently inside a parenthesis context and would have to exit that context before it’s able to finish with a valid result

which would mean that if I always remember to put all parenthesis I’ll get more specific errors.
and I’m guessing you will say that writing the code correctly form the beginning isn’t the idea, but finding the mistake when it happens is.
or something like that.

No I agree that you should be inserting pairs or the whole thing for other things, and then fill them out later, to maintain some integrity at all times.

But if something is wrong regardless then one should be in sufficient control to spot it, there shouldn’t be anything in the code that I don’t know what it’s supposed to look like.

Debugging code that runs but does something wrong is the same thing isn’t it. I should know what’s being done well enough that I can make observations and find where it goes wrong. The alternative is pretty silly.

And if I misunderstand what something does (meaning something isn’t under my control despite my best efforts) then I better be able to strip things down and isolate that behaviour. Since I had an idea of what it should have done that also gives me something to compare with, meaning I’ll find the difference.

You also need to know what the nature of the thing you’re manipulating is. Otherwise you won’t know what you can do with it. If you know what something is you can figure out what makes sense to do with it

I might sound a bit conservative, it’s not that I’m saying anyone should be going slow.
What I am saying is that the more control one has the faster one can go. Otherwise one just gets stuck with no firm ground to use to keep moving.

The Python interpreter will allow you to either complete the expression or to continue building it. Ultimately, you will have to complete it, though. If, prior to completion of the expression, you put any code in its trajectory that is inconsistent with continuing or completing the expression, a SyntaxError will be raised. If the program ends while the interpreter still considers the expression to be open, you will see a message stating something similar to unexpected EOF while parsing. EOF refers to the end of the file.

k.o. commander.
Iv’e got the point.

1 Like