无线蟒蛇

Fen*_*tik 1 python output

我试图使用随机打印随机数,但是当我尝试使用输出在一行中打印end= " "输出时不显示任何内容,直到我打破程序.

import random
import time
while True:
    x = random.randint(1,6)
    print(x, end=" ")
    time.sleep(1)
Run Code Online (Sandbox Code Playgroud)

我打断后输出是这样的:

C1 2 3 5 5 4 5 4 1 ---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
Run Code Online (Sandbox Code Playgroud)

DJV*_*DJV 5

您可以通过传递flush=Trueprint函数来禁用缓冲(在python3中)

print(x, end=" ", flush=True)
Run Code Online (Sandbox Code Playgroud)