以下代码在Python 2和Python 3中工作。
但是对于python3,可以看到ramchandraapte的好答案。在from sys import stdout
from time import sleep
for i in xrange(5,0,-1):
stdout.write(str(i))
sleep(1)
stdout.write(' Blastoff')
为了使此代码在命令行窗口中执行,str(i)是必需的。在editshell窗口中,可以写入stdout.write(i),但是'\b'信号不会将光标向后移动,而是显示一个正方形代替它。在
一。在
顺便说一下,试试下面的代码看看会发生什么。
'\b'触发光标向后移动一个位置,替换前面的字符,因此每个字符都只在前面的那个位置写入。它只在命令行窗口中工作,在编辑shell窗口中不起作用。在
^{pr2}$
复杂化(仍要在命令行窗口中执行):from sys import stdout
from time import sleep
tu = (' End in 24 seconds',
' It remains 20 seconds',
' Still only 16 seconds',
' Do you realize ? : 12 seconds left !',
' !!!! Hey ONLY 8 seconds !!')
for s in tu:
stdout.write('*')
sleep(1)
stdout.write(s)
sleep(2)
stdout.write(len(s)*'\b' + len(s)*' ' + len(s)*'\b')
sleep(1)
stdout.write(len(tu)*'\b' + len(tu)*'!' + ' ')
n = 4
for x in xrange(n,0,-1):
stdout.write('\b!'+str(x))
sleep(1)
stdout.write(len(tu)*'\b' + n*'\b' + '\b')
stdout.write(' # !! BLASTOUT !! #\n')
sleep(0.8)
stdout.write(' ## !! BLASTOUT !! ##\n')
sleep(0.8)
stdout.write(' ### !! BLASTOUT !! ##\n')
sleep(0.8)
stdout.write(' #### !! BLASTOUT !! ####\n')
sleep(3)