为什么相同的代码会有不同的效果?

Why does the same code works differently?

好吧,我读到标准输出是行缓冲的。但代码在 Pydroid 3(不知道确切版本)和 Python 3.8.3.

中的工作方式不同
import time
print('Hello', end = '')
time.sleep(5)
print('World')

在 Pydroid 3 中,Hello 和 World 都在(至少在)5 秒后打印,而在 Python3.8.3 中,首先打印 Hello,5 秒后打印 World。

为什么代码的工作方式不同?

这可能不是 Python 版本问题,而是不同的终端问题。

一些终端(或更准确地说 files/streams,包括 stdout)仅在换行后刷新(第一个 print 没有),而其他终端可以在每次刷新后刷新写.

要强制冲洗,您可以使用 flush=True as a param to print,试试这个:

import time
print('Hello', end='', flush=True)
time.sleep(5)
print('World')