这是我的 Python 3 代码:
from time import sleep
s='what is your name'
for x in s:
print(x,end='')
sleep(1)
我期望的是,每个字母会在延迟一秒后一个一个地打印出来。但是当我运行这个程序时,它需要 len(s) 秒的时间,然后打印出 s 的值。 谁能给我解释一下到底发生了什么。
请您参考如下方法:
使用 flush 参数到 print 函数:
print(x, end='', flush=True)
来自 docs :
Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.
