为什么不像打印结束论证那样sleep time.sleep工作?

发布于 2025-02-14 00:13:21 字数 323 浏览 1 评论 0原文

我正在尝试使用time.sleep()在打印语句之间暂停。

import time
def test():
    print("something", end="")
    time.sleep(1)
    print(" and ", end="")
    time.sleep(1)
    print("something")

当我使用最终参数时,代码会等待它才开始打印。它应该打印,等待,打印,等待和打印(“某物,等待,等等,等等”)。但是,当我使用最终参数时,它会等待并打印“等待某事”。该代码可以按照我想要的方式工作,而无需结束argumets。

I am trying to use time.sleep() to pause in between print statements.

import time
def test():
    print("something", end="")
    time.sleep(1)
    print(" and ", end="")
    time.sleep(1)
    print("something")

When I use the end argument, the code waits before it starts to print. It should print, wait, print, wait, and print ("something, wait, and, wait, something"). However, when I use the end argument, it waits, and prints "wait something and something". This code works as I wanted it to without the end argumets.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谷夏 2025-02-21 00:13:21

您的输出设备是缓冲的,只有在输出新线路时才冲洗。将flush = true作为附加参数添加到print将冲洗施加到输出。

Your output device is line-buffered and only flushes when a new line is output. Add flush=True as an additional parameter to print to force a flush to the output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文