【发布时间】:2016-01-06 12:58:04
【问题描述】:
此代码按预期工作。输出:
Loading
Loading.
Loading..
Loading...
代码:
done = False
count = 0
while not done:
print '{0}\r'.format("Loading"),
time.sleep(0.25)
print '{0}\r'.format("Loading."),
time.sleep(0.25)
print '{0}\r'.format("Loading.."),
time.sleep(0.25)
print '{0}\r'.format("Loading..."),
time.sleep(0.25)
count += 1
if count == 5:
done = True
而这段代码没有。输出:
Loading.
Loading...
代码:
done = False
count = 0
while not done:
print '{0}\r'.format("Loading"),
time.sleep(0.125)
print '{0}\r'.format("Loading."),
time.sleep(0.125)
print '{0}\r'.format("Loading.."),
time.sleep(0.125)
print '{0}\r'.format("Loading..."),
time.sleep(0.125)
count += 1
if count == 5:
done = True
如果时间函数低于 0.25,为什么似乎每秒都跳过print 语句?
【问题讨论】:
-
奇怪,它可以在我的机器上运行。也许它取决于操作系统。我使用的是 Windows 7。您使用的是什么操作系统?
-
我没有这个问题,我得到了
Loading Loading. Loading.. Loading...,但很可能与您的回车有关。 -
好像和输出缓冲有关,不同机器上效果会有很大差异。例如,它根本没有在我的机器上打印任何东西。 :-)
-
如果你导入打印函数或切换到 Python 3,你可以只使用
flush=True而不是显式调用sys.stdout.flush()。 -
@TigerhawkT3:您不能使用
__future__导入来获得Py2 中的flush=True支持;flush参数仅添加到 Py 3.3 中的print函数中,并且从未向后移植到 2.x 行。
标签: python python-2.7 time stdout