Indentation

GWYoder yodersc at erols.com
Tue Jun 19 10:50:43 EDT 2001


I'm still trying here is what I'm getting...

Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> y = 1900
>>> leap = "no"
>>> if y % 400 == 0:
 leap = "yes"
 elif y % 100 == 0:

SyntaxError: invalid syntax

"Chris Gonnerman" <chris.gonnerman at newcenturycomputers.net> wrote in message
news:mailman.992955149.25097.python-list at python.org...
> ----- Original Message -----
> From: "Pilgrim" <gwyoder at hotmail.com>
>
>
> > I'm new to programming so take it easy on me :-)
> >
> > I'm going through a book by Ivan Van Laningham and have gotten to
> > indentations.  In the example I'm trying duplicate he as written three
> lines
> > from the command prompt.  After the third line is a new indentation and
> then
> > the next line starts back at the beginning without the command prompt
>>>
> > and starts with an "elif" statement.  According to his tip to start a
new
> > line you just hit return twice when you are done with the indentation.
OK
> > that works fine and starts me at a new prompt >>> but when I type the
next
> > line starting with a "elif" statement it gives me a SyntaxError: invalid
> > syntax.  What I'm I missing?
>
> You've got it *almost* right.  When you are typing a control-flow
> structure at the interactive prompt, you *don't* press enter twice
> before the next control-flow keyword.  To wit:
>
> Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license" for more information.
> Alternative ReadLine 1.4 -- Copyright 2001, Chris Gonnerman
> >>> t = 1
> >>> if t == 1:
> ...     print "one"
> ... elif t == 2:
> ...     print "two"
> ... else:
> ...     print "something else"
> ...
> one
> >>>
>
> What you are doing is this:
>
> >>> if t == 1:
> ...     print "one"
> ...
> one
> >>> elif t == 2:
>   File "<stdin>", line 1
>     elif t == 2:
>        ^
> SyntaxError: invalid syntax
> >>>
>
> Pressing Enter twice ends the code block, but the if ... elif ... else
> structure must not be broken up this way.
>
>





More information about the Python-list mailing list