【问题标题】:Time sleep not working in loop after printing a line [duplicate]打印一行后,时间睡眠不能循环工作[重复]
【发布时间】:2019-12-17 10:11:02
【问题描述】:
import time
while True:
    time.sleep(1)
    print(".", end=" ")

我只是想通过在一秒钟后出现点来制作一种加载屏幕,我期待 这-

.{wait}.{wait}.{wait}.{wait}

但是我没有得到任何输出,但是通过 ctrl + c 关闭程序后,我得到了这样的输出-


. . . . . . . . . . . . Traceback (most recent call last):
  File "c:/Users/Piyush/Desktop/Python/dotdotdotload.py", line 12, in <module>
    time.sleep(1)
KeyboardInterrupt```

【问题讨论】:

  • 尝试使用print(".", end=" ", flush=True)
  • 它有效,XD 非常感谢!

标签: python python-3.x python-2.7


【解决方案1】:

你需要刷新输出,它会显示:

Python-3.x:https://repl.it/repls/DarksalmonFussyBooleanalgebra

import time

while True:
    time.sleep(1)
    print(".", end=" ", flush=True)

Python-2.7:https://repl.it/repls/CuteTenseRoutes

import sys
import time

while True:
    time.sleep(1)
    print ". ",
    sys.stdout.flush()

【讨论】:

  • 感谢您为编写大量代码所做的努力:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
  • 2019-02-04
  • 2013-11-30
相关资源
最近更新 更多