pythonterminal

How can I write multi-line code at the Python REPL (in a terminal window)?


Suppose I run Python from a terminal window and get the REPL prompt:

python

Output:

Python 2.7.10 (default, Jul 30 2016, 19:40:32)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

I want to input some code like:

i = 0

while i < 10:
    i += 1
    print i

How do I actually type the newlines and indent the code?

I tried using Control+Enter, Shift+Enter, and Command+Enter; none of them worked. The code does not indent:

>>> while i < 10:
... print i
  File "<stdin>", line 2
    print i
        ^
IndentationError: expected an indented block

Solution

  • 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