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

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página