Indentation
Chris Gonnerman
chris.gonnerman at newcenturycomputers.net
Tue Jun 19 08:58:04 EDT 2001
----- 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