![](/img/trans.png)
[英]Why doesn't `print`ed output on the same line appear between `time.sleep` calls?
[英]Why doesn't time.sleep work as with print end arguments?
我正在尝试使用 time.sleep() 在打印语句之间暂停。
import time
def test():
print("something", end="")
time.sleep(1)
print(" and ", end="")
time.sleep(1)
print("something")
当我使用 end 参数时,代码会在开始打印之前等待。 它应该打印、等待、打印、等待和打印(“某事,等等,等等,某事”)。 但是,当我使用 end 参数时,它会等待并打印“等待某事和某事”。 这段代码在没有结束参数的情况下按我想要的方式工作。
您的 output 设备是行缓冲的,并且仅在新行为 output 时刷新。 添加flush=True
作为附加参数以强制刷新到print
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.