How can I write multi-line code in the Terminal use python?
There comes out:
IndentationError: expected an indented block
So, when use the while loop, the next line should have the indented block(press Tab key).
>>> i = 0
>>> while i < 10:
... i += 1
... print i
...
1
2
3
4
5
6
7
8
9
10
>>>
Just copy the code and past it in the terminal, and press return. This code works perfect if you do that:
i = 0
..
.. while i < 10:
.. i += 1
.. print(i)
..
1
2
3
4
5
6
7
8
9
10
You can add a trailing backslash. For example, if I want to print a 1:
>>> print 1
1
>>> print \
... 1
1
>>>
If you write a \, Python will prompt you with ... (continuation lines) to enter code in the next line, so to say.
To resolve IndentationError: expected an indented block
, put the next line after while loop in an indented block (press Tab key).
So, the following works:
>>> i=0
>>> while i < 10:
... i+=1
... print i
...
1
2
3
4
5
6
7
8
9
10