美文网首页大数据 爬虫Python AI SqlPython小哥哥
Python的while循环做倒计时秒表和用random库做幸运

Python的while循环做倒计时秒表和用random库做幸运

作者: 14e61d025165 | 来源:发表于2019-04-13 14:55 被阅读1次

    做一个一分钟倒计时的秒表,思路是这样的,从60开始,每隔0.1秒倒数一个数,那么60个0.1秒就正好是一分钟,这里我们要用到time库的sleep方法,所以要先引入time库,代码如下:

    一分钟倒计时

    import time

    itime=60

    while itime>=0:

    print("\r{:2}".format(itime), end="")
    

    itime=itime-1

    time.sleep(0.1)

    运行结果动态图

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555138452607 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    接下来我们把上述源代码做一个调整,把输出的60个整数,改成我们班同学的学号,这个学号我们用random来随机生成,只需给出起始学号和中杰学号就可以了,60秒的时间倒计时里,系统会随机生成学生的学号,最后停留的那个号码就是我们想要的幸运号码。代码如下:

    谁最幸运,随机找出你的学号

    import time

    import random

    itime=60

    while itime>=0:

    print("\r幸运号码就是你:1820040{:2}".format(random.randint(3101,3136)), end="")

    itime=itime-1

    time.sleep(0.01)

    运行结果动态图

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555138452613 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    Python学习群:683380553,有大牛答疑,有资源共享!有想学习python编程的,或是转行,或是大学生,还有工作中想提升自己能力的,正在学习的小伙伴欢迎加入学习。

    相关文章

      网友评论

        本文标题:Python的while循环做倒计时秒表和用random库做幸运

        本文链接:https://www.haomeiwen.com/subject/haolwqtx.html