Sub topic 6
Multi-line statements and the ... prompt
This chapter assumes you can write Python loops, conditionals, and functions, and that you can start the shell with python3. You will learn how the shell decides a statement is unfinished, how to end a block, and which mistakes cause errors.
Two prompts
The shell shows >>> when it waits for a new statement. When what you typed is not yet complete, it shows ... and waits for more. The ... prompt means "continue this statement."
A line that ends in a colon starts a compound statement, such as for, if, def, or while. The shell then needs the indented body.
>>> for i in range(3):
... print(i * i)
...
0
1
4You indent the body yourself. The shell does not add indentation in the classic prompt. An empty line (just Enter) tells the shell the block is finished, and it runs the whole statement at once.
Defining functions
A def works the same way. The definition runs when you press Enter on the empty line. After that the name exists for the rest of the session.
>>> def square(n):
... return n * n
...
>>> square(7)
49The blank line rule
The blank line is part of the syntax at this prompt. It marks where a block ends, so you cannot skip it. If you type the next statement straight after the body, the shell treats it as part of the same unfinished statement and reports a SyntaxError.
>>> def square(n):
... return n * n
... square(7)
File "<stdin>", line 3
square(7)
^^^^^^
SyntaxError: invalid syntaxThe exact wording of the message varies between Python versions. The cause is the same: the missing blank line.
The same rule affects if/else. Keep else on the line right after the body, with no blank line between them.
>>> n = 4
>>> if n % 2 == 0:
... print("even")
... else:
... print("odd")
...
evenIf you pressed Enter on an empty line before else, the if would run on its own. The else would then be a SyntaxError.
Nested blocks
You can nest blocks by indenting further. A single empty line ends all of them at once.
>>> for n in range(4):
... if n % 2 == 0:
... print(n, "even")
...
0 even
2 evenOther reasons you see `...`
Indented blocks are not the only way to get a continuation prompt. The shell also waits for more input when a bracket, parenthesis, or brace is still open, or when a line ends with a backslash. A triple-quoted string that has not been closed does the same.
>>> total = (1 +
... 2 +
... 3)
>>> total
6Here no blank line is needed. The statement is complete as soon as the closing parenthesis is typed. Because the shell keeps waiting, a forgotten closing bracket makes it look stuck at .... Close the bracket, or press Ctrl+C to abandon the statement and return to >>>.
Common errors
| What you see | Likely cause | Fix |
|---|---|---|
| `IndentationError: expected an indented block` | The body line was not indented | Indent the body, usually by four spaces |
| `SyntaxError` on the line after a block | No blank line to end the block | Press Enter on an empty line first |
| `SyntaxError` on `else` | A blank line came before `else` | Put `else` directly after the `if` body |
| Stuck at `...` | An open bracket or string | Close it, or press Ctrl+C |
Exercise
You type def greet(name):, then return "hi " + name, and then greet("Ana") directly on the next line. The shell reports a SyntaxError. What went wrong, and what do you type to fix it?
Show answer
The function body was never ended, so the shell read greet("Ana") as part of the same def statement. Press Enter on an empty line after the return line to finish the definition. Then call greet("Ana") at the >>> prompt.