[Tutor] Syntax error while attempting to type in multiline statements in the interactive interpreter

David bouncingcats at gmail.com
Sat Feb 4 17:40:12 EST 2017


On 5 February 2017 at 09:02, boB Stepp <robertvstepp at gmail.com> wrote:
> py3: a
> ['Mary', 'had', 'a', 'little', 'lamb', 'break']
> py3: for w in a:
> ...     print(w)
> ... print('Huh?')
>   File "<stdin>", line 3
>     print('Huh?')
>         ^
> SyntaxError: invalid syntax
>
> I don't understand why this throws a SyntaxError.  If I wrap
> essentially the same code into a function it works:

>From [1]: "When a compound statement is entered interactively, it must
be followed by a blank line to indicate completion (since the parser
cannot guess when you have typed the last line). Note that each line
within a basic block must be indented by the same amount."

Does that help?

[1] https://docs.python.org/3/tutorial/introduction.html


More information about the Tutor mailing list