[Tutor] what is wrong with this syntax?

spir ☣ denis.spir at gmail.com
Tue May 18 19:29:46 CEST 2010


On Wed, 19 May 2010 01:23:55 +1000
Steven D'Aprano <steve at pearwood.info> wrote:

> On Tue, 18 May 2010 11:36:02 pm Dipo Elegbede wrote:
> > ples help me figure out what is wrong with this syntax?
> >
> >
> > print('Here are the numbers from 0 to 9')
> > for i in the range(10):
> >     print(i)
> >
> > thank you.
> 
> Others have already given you the answer, but more important is for you 
> to learn *how* to get the answer.
> 
> Look at the error message Python prints:
> 
> >>> for i in the range(10):
>   File "<stdin>", line 1
>     for i in the range(10):
>                      ^
> SyntaxError: invalid syntax
> 
> 
> You get a SyntaxError, which tells you that what you've written makes no 
> sense to the Python compiler. It also tells you that the error has 
> nothing to do with either of the print lines.
> 
> Unfortunately Python isn't smart enough to recognise that the problem is 
> with "the" rather than "range(10)", but it points you to the correct 
> line.

And logically, if the error is not at the pointed word/line, it will be just before.

This means that when analysing your code, python passed on the real error cause, because of a possible ambiguity; but then the rest makes no sense, so it stops and points where it got blocked, immediately after the error.
In the code above, "the" could be a name you gave to a list, for instance; since an expression starting like "for i in listName" is correct, Python cannot stop on "the"... but then the rest of the line makes no more sense for it.

(Fortunately, python 3.2, planned for April 1, 2011, will be informed that "the" is an english article. This is possible since there is no ambiguity with "thé" (fr), thank to Python's clever diacritic-awareness. Only remains then the problematic case of "a".)


Denis
________________________________

vit esse estrany ☣

spir.wikidot.com


More information about the Tutor mailing list