[파이썬] `sys.ps2`: 이어지는 입력 라인의 프롬프트

To set a custom sys.ps2, you need to import the sys module and modify the ps2 attribute. Here’s an example:

import sys

sys.ps2 = ">>> "

print("Enter a multi-line code block:")
code_block = ""
line = input(sys.ps2)
while line:
    code_block += line + "\n"
    line = input(sys.ps2)

print("Executing code block:")
exec(code_block)

In this example, we first import the sys module. Then we set sys.ps2 to the desired prompt, which in this case is ">>> ".

Next, we ask the user to enter a multi-line code block. We initialize an empty string code_block to store the complete code. We then use a loop to continuously read lines of input until the user enters an empty line. Each line is appended to code_block along with a newline character.

Once the user finishes entering the code block, we print a message indicating that the code block is being executed. We use the exec() function to execute the code stored in code_block.

By customizing sys.ps2, you can make the prompt more visually appealing or provide additional information to guide the user when entering multi-line code blocks.