【问题标题】:Why is my sleep not sleeping when i say but before I say in python [duplicate]为什么当我说但在我用python说之前我的睡眠不睡觉[重复]
【发布时间】:2020-09-09 08:50:05
【问题描述】:

我在下面给出了代码,它在 python 中

from time import *
# I dont need to use time every time 
print("hi" ,end="")
sleep(3)
print("." ,end="")
sleep(3)
print("." ,end="")
sleep(3)
print("." )

我打个招呼...但是在打印任何东西之前需要 9-10 秒并且中间没有任何间隙

【问题讨论】:

  • 检查flushprint() 参数。
  • 这在我不使用 end="" 时有效,但我需要它在同一行
  • 认为它有效我对 python 很陌生,所以我只知道很少的东西,但我正在学习它我有点努力,因为我 13 年感谢你的帮助
  • 等待我不能知道,因为这不允许知道,它让我等待!!
  • flush在我们添加的时候做了什么,你能解释一下它的功能吗

标签: python sleep


【解决方案1】:

flush 参数设置为True 会使您的print() 语句及时打印。

from time import sleep

print("hi", end="", flush=True)
sleep(3)

for i in range(3):
    print(".", end="", flush=True)
    sleep(3)

【讨论】:

  • "当用于指示关键字参数或用于指示未注释函数参数的默认值时,请勿在 = 符号周围使用空格" © @987654321 @.
  • “每个缩进级别使用 4 个空格” © PEP 8.
  • 感谢您的所有建议,当我复制粘贴到在线 python ide 中时,我的代码已自动格式化。
猜你喜欢
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
  • 2020-08-05
  • 2021-01-15
  • 1970-01-01
  • 2013-05-28
相关资源
最近更新 更多