[Tutor] what is wrong with this syntax?

bob gailer bgailer at gmail.com
Tue May 18 19:18:13 CEST 2010


On 5/18/2010 11:23 AM, Steven D'Aprano wrote:
> 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)"
>    

To be more specific - Python is "happy" with "for i in the ". It is 
"expecting"either : or some operator. "range" is neither - so that is 
where the error pointer is.

Example:

the = [1,2,3]
for i in the:
   print(i)
for i in the + [4]:
   print(i)

-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the Tutor mailing list