[Python]为何延时函数在后面,前面的输出仍然要等到延时后?

软件和网站开发以及相关技术探讨
回复
科学之子
帖子: 2284
注册时间: 2013-05-26 6:58
系统: Debian 9

[Python]为何延时函数在后面,前面的输出仍然要等到延时后?

#1

帖子 科学之子 » 2017-02-17 17:52

[Python]为何延时函数在后面,前面的输出仍然要等到延时后?

代码: 全选

#!/usr/bin/python3
import time
s='Surround'
while True :
    for x in s:
        print (x,end='')
    time.sleep(1)
    print('\r',end='')
头像
vickycq
帖子: 4507
注册时间: 2011-03-20 13:12
系统: Debian
来自: 山东省寿光县
联系:

Re: [Python]为何延时函数在后面,前面的输出仍然要等到延时后?

#2

帖子 vickycq » 2017-02-17 18:37

代码: 全选

#!/usr/bin/python3
import time
import sys

s = 'Surround'
while True :
    for x in s:
        print(x, end='')
        sys.stdout.flush()
        time.sleep(0.1)

    time.sleep(1)
    print('\r', end='')

代码: 全选

#!/usr/bin/python3
import time

s = 'Surround'
while True :
    for x in s:
        print(x, end='', flush=True)
        time.sleep(0.1)

    time.sleep(1)
    print('\r', end='')
Help on built-in function print in module builtins:

print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
Debian 中文论坛 - forums.debiancn.org
欢迎所有 Debian GNU/Linux 用户
回复