• 技术文章 >Python技术 >Python基础教程

    Python动态循环的使用

    2021-03-24 16:22:06原创2861

    有时候我们需要一些动态循环的字幕滚动展示,那么在Python中怎样实现呢?

    一、实例:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;">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></span></p>

    二、结果:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;">新春佳节快乐

    请勿越过白色安全线!

    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></span></p>

    说明:时间可以取当日。

    以上就是Python动态循环的使用方法。更多Python学习推荐:PyThon学习网教学中心

    (推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)


    专题推荐:python动态循环
    上一篇:Python中numpy.where()函数的使用 下一篇:详解python中的ASCII码转为字符

    相关文章推荐

    • Python中numpy求函数的导数实现方法• Python中numpy如何进行降序?• Python中用numpy进行图片处理• Python中numpy如何构建多维数组• Python中numpy多维数组的用法• Python中numpy.where()函数的使用

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网