如何在Python中创建打印速度较慢的代码

2024-06-17 10:32:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了这些函数,但它们不起作用,即使使用如下定义的sayNoLine函数:

def sayNoLine (x):
     print (x, end = '')

def sayNoLine (x):
     sys.stdout.write (x)

def printSlowly (* args, Thetime = 0.5):
     tt = getTime ()
     txt = ''
     for i in ranlen (args):
         txt + = str (args [i])
     print ('first time: {0}'. format (tt-getTime ()))
     for time in ranlen (txt):
         sayNoLigne (txt [time])
         att (thetime)
     print ()
     print ('second time: {0}'. format (getTime () - tt))

每次代码在显示文本之前等待print()函数(因此换行):

print ( 't' end = '')
# output:
print ( 'e', end = '')
# output:
print ( 's' end = '')
# output:
print ( 't' end = '')
# output:
print ()
# output: test

Tags: 函数intxtformatforoutputtimedef