有时候我们需要一些动态循环的字幕滚动展示,那么在Python中怎样实现呢?
一、实例:
<p style="line-height: 1.75em;">import sys
print('\n'+'2021年02月06日 17:30:36' + '\r')
print_act('请勿越过白色安全线!')<br></p>
<p style="line-height: 1.75em;">import sys
import time
def print_act(word):
print('新春佳节快乐'+'\r')
#让光标回到行首
sys.stdout.write("\r")
#缓冲区的数据全部输出
sys.stdout.flush()
#遍历整个单词
for item in word:
#写到缓冲区
sys.stdout.write(item)
#输出
sys.stdout.flush()
#暂停0.3秒
time.sleep(0.3)
print('\n'+'2021年02月06日 17:30:36' + '\r')
while True:
#调用print_act方法
print_act('请勿越过白色安全线!')<br></p>
import sys
import time
def print_act(word):
print('新春佳节快乐'+'\r')
#让光标回到行首
sys.stdout.write("\r")
#缓冲区的数据全部输出
sys.stdout.flush()
#遍历整个单词
for item in word:
#写到缓冲区
sys.stdout.write(item)
#输出
sys.stdout.flush()
#暂停0.3秒
time.sleep(0.3)
print('\n'+'2021年02月06日 17:30:36' + '\r')
while True:
#调用print_act方法
print_act('请勿越过白色安全线!')
二、结果:
<p style="line-height: 1.75em;">新春佳节快乐
<p style="line-height: 1.75em;">新春佳节快乐
请勿越过白色安全线!
2021年02月06日 17:30:36
新春佳节快乐
请勿越过白色安全线!
2021年02月06日 17:30:36
新春佳节快乐
请勿越过白色安全线!
2021年02月06日 17:30:36
新春佳节快乐
请勿越过白色安全线!
2021年02月06日 17:30:36
………………(此处会一直循环)<br></p>
新春佳节快乐
请勿越过白色安全线!
2021年02月06日 17:30:36
新春佳节快乐
请勿越过白色安全线!
2021年02月06日 17:30:36
新春佳节快乐
请勿越过白色安全线!
2021年02月06日 17:30:36
新春佳节快乐
请勿越过白色安全线!
2021年02月06日 17:30:36
………………(此处会一直循环)
说明:时间可以取当日。
以上就是Python动态循环的使用方法。更多Python学习推荐:PyThon学习网教学中心。
(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)