aircraft
aircraft

Reputation: 26886

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:

aircraftdeMacBook-Pro:~ ldl$ python
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

Upvotes: 22

Views: 55256

Answers (7)

giusti
giusti

Reputation: 3538

You can just paste the code. However, if the pasted code contains empty lines, it may fail. For example, pasting the following causes an infinite loop:

i = 0
while i < 10:
    print(i)

    i += 1

That's because the standard REPL starts running the code as soon as it encounters a blank line, which it treats as a signal that the code has ended. So in the example above, it will never read the increment.

There are some workarounds, but no guaranteed way to do this. You can run the pasted code into exec, but it will fail if the pasted code contains a triple-quoted string:

exec("""i = 0

while i < 10:
    print(i)

    i += 1""")

If you don't want to rely on exec, then you will need to change the code you are pasting. Either remove the blank lines or replace them with indentation or a single space.

i = 0

while i < 10:
    print(i)
 # this comment not needed; this is only here 
 # to emphasize that this line is not really empty
    j = 0
    while j < 2:
        print(i, j)
 #
        j += 1
 #
    i += 1

The silver lining is that you can easily do this in an editor that allows you to replace empty lines with a space.

Another trick is to make every line start with some indentation and then add a dummy if at the beginning:

if True:
    i = 0
    #
    while i < 10:
        print(i)
    # 
        i += 1

But again, there's no sure way to just copy and paste into REPL without some work. If that's a deal-breaker, then it's best to just use IPython.

Upvotes: 2

backslash001
backslash001

Reputation: 65

Maybe I'm a little late, but you can try this

$: echo "\
i = 0

while i < 10:
    i += 1
    print i 
" | python3

Upvotes: 1

Cyril Gupta
Cyril Gupta

Reputation: 13723

Python automatically detects code blocks in sections like for-next, while, etc. Just put a ':' <-- Colon symbol after some code.

Then the next line will have a continuation symbol ('...') in front of it instead of the prompt ('>>>')

Remember to press a tab to indent the code that you want to execute in the block. That will indent the line and tell Python that the code that follows is a part of the block.

Upvotes: 1

anshul singh
anshul singh

Reputation: 29

Utilize the python3 - <<'EOF' command.

For instance:

python3 - <<'EOF'
a=7
b=5
print(a+b)
EOF

12

Upvotes: 2

Wasi Ahmad
Wasi Ahmad

Reputation: 37691

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

Upvotes: 16

aircraft
aircraft

Reputation: 26886

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
>>> 

Upvotes: 4

developer_hatch
developer_hatch

Reputation: 16224

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

Upvotes: 0

Related Questions