动态加色打印,\033控制,显示进行到的百分比 ,实时更新总共用时。
(本笔记适合能熟练应用字符串和循环技能的 coder 翻阅)
【学习的细节是欢悦的历程】
自学并不是什么神秘的东西 ,一个人一辈子自学的时间总是比在学校学习的时间长,没有老师的时候总是比有老师的时候多。 —— 华罗庚
动态加色打印,\033控制
动态文本进度条
(显示进行到的百分比,实时更新总共用时)
本文质量分:
【 96 】
本文地址:
https://blog.csdn.net/m0_57158496/article/details/133280609
CSDN质量分查询入口:http://www.csdn.net/qc
目 录
◆ 动态文本进度条
1、题目描述
回页目录
2、算法解析
2.1 复刻提问中的代码
我看不出代码有啥毛病,于是我手打问题中的代码,对其进行验证,结果仅仅是进度条宽度对于我的手机太大了,代码本身没有任何问题。
效果 进行中…… 完成后 经无序繁星 答主回答提醒,老发题主是在python 2.x上跑3.x语法的代码。粗心如我,居然没看出来。😂
代码
import time
scale = 50
print ( 'scation' . center( scale// 2 , '-' ) )
start = time. perf_counter( )
for i in range ( scale+ 1 ) :
a, b = '*' * i, '.' * ( scale- i)
c = ( i/ scale) * 100
dur = time. perf_counter( ) - start
print ( "\r{:3.0f}%[{} -> {}] {:.2f}s" . format ( c, a, b, dur) , end= '' )
time. sleep( 0.1 )
print ( '\n' + 'end' . center( scale// 2 , '-' ) )
回页目录
2.2 代码优化
可以直接用time模块的time、sleep方法,用\033控制码打印颜色,更漂亮。(颜色控制仅Linux 有效) 动态变化中ing… 完成的进度条
代码
from time import time
from time import sleep
blue, off = '\033[31;43m' , '\033[0m'
scale = 42
print ( f"\n { ' Scation ' : ~^54 } \n\n { blue} " )
start = time( )
for i in range ( scale+ 1 ) :
print ( f" { ( i/ scale) * 100 : 3.0f } %[ { '*' * i} { '.' * ( scale- i) } ] { time( ) - start: .2f } " , end= '\r' )
sleep( 0.2 )
print ( f" { off} \n\n\n { ' End ' : ~^54 } \n" )
回页目录
## 3、完整源码
(源码较长,点此 跳过源码)
import time
scale = 36
print ( 'scation' . center( scale// 2 , '-' ) )
start = time. perf_counter( )
for i in range ( scale+ 1 ) :
a, b = '*' * i, '.' * ( scale- i)
c = ( i/ scale) * 100
dur = time. perf_counter( ) - start
print ( "\r{:3.0f}%[{} -> {}] {:.2f}s" . format ( c, a, b, dur) , end= '' )
time. sleep( 0.1 )
print ( '\n' + 'end' . center( scale// 2 , '-' ) )
from time import time
from time import sleep
blue, off = '\033[31;43m' , '\033[0m'
scale = 42
print ( f"\n { ' Scation ' : ~^54 } \n\n { blue} " )
start = time( )
for i in range ( scale+ 1 ) :
print ( f" { ( i/ scale) * 100 : 3.0f } %[ { '*' * i} { '.' * ( scale- i) } ] { time( ) - start: .2f } " , end= '\r' )
sleep( 0.2 )
print ( f" { off} \n\n\n { ' End ' : ~^54 } \n" )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
回页目录
3、我的“相关”博文笔记
回页首
上一篇: 常用圆圈字符&“圆圈字符替换器” (本文收录了162个常用圆圈字符,文内有“圆圈字符自动替换器”) 下一篇:
我的HOT 博:
本次共计收集 240 篇博文笔记信息,总阅读量 40.17w,平均阅读量 1673。已生成 27 篇阅读量不小于 3000 的博文笔记索引链接。数据采集于 2023-09-25 03:28:55 完成,用时 4 分 13.96 秒。
ChatGPT国内镜像站初体验:聊天、Python代码生成等 ( 58867 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/129035387 点赞:125 踩 :0 收藏:796 打赏:0 评论:71 本篇博文笔记于 2023-02-14 23:46:33 首发,最晚于 2023-07-03 05:50:55 修改。 让QQ群昵称色变的神奇代码 ( 57958 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/122566500 点赞:24 踩 :0 收藏:83 打赏:0 评论:17 本篇博文笔记于 2022-01-18 19:15:08 首发,最晚于 2022-01-20 07:56:47 修改。 pandas 数据类型之 DataFrame ( 9120 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/124525814 点赞:6 踩 :0 收藏:31 打赏:0 评论:0 本篇博文笔记于 2022-05-01 13:20:17 首发,最晚于 2022-05-08 08:46:13 修改。 个人信息提取(字符串) ( 7144 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/124244618 点赞:1 踩 :0 收藏:13 打赏:0 评论:0 本篇博文笔记于 2022-04-18 11:07:12 首发,最晚于 2022-04-20 13:17:54 修改。 罗马数字转换器|罗马数字生成器 ( 6990 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/122592047 点赞:0 踩 :0 收藏:1 打赏:0 评论:0 本篇博文笔记于 2022-01-19 23:26:42 首发,最晚于 2022-01-21 18:37:46 修改。 Python字符串居中显示 ( 6867 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/122163023 点赞:1 踩 :0 收藏:7 打赏:0 评论:1 本篇博文笔记 Python列表(list)反序(降序)的7种实现方式 ( 6706 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/128271700 点赞:5 踩 :0 收藏:20 打赏:0 评论:8 本篇博文笔记于 2022-12-11 23:54:15 首发,最晚于 2023-03-20 18:13:55 修改。 斐波那契数列的递归实现和for实现 ( 5500 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/122355295 点赞:4 踩 :0 收藏:2 打赏:0 评论:8 本篇博文笔记 练习:字符串统计(坑:f‘string‘报错) ( 5074 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/121723096 点赞:0 踩 :0 收藏:1 打赏:0 评论:0 本篇博文笔记 python清屏 ( 5031 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/120762101 点赞:0 踩 :0 收藏:8 打赏:0 评论:0 本篇博文笔记 回车符、换行符和回车换行符 ( 5026 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/123109488 点赞:1 踩 :0 收藏:2 打赏:0 评论:0 本篇博文笔记于 2022-02-24 13:10:02 首发,最晚于 2022-02-25 20:07:40 修改。 练习:尼姆游戏(聪明版/傻瓜式•人机对战) ( 4908 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/121645399 点赞:14 踩 :0 收藏:42 打赏:0 评论:0 本篇博文笔记 密码强度检测器 ( 4286 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/121739694 点赞:1 踩 :0 收藏:4 打赏:0 评论:0 本篇博文笔记于 2021-12-06 09:08:25 首发,最晚于 2022-11-27 09:39:39 修改。 练习:生成100个随机正整数 ( 4221 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/122558220 点赞:1 踩 :0 收藏:6 打赏:0 评论:0 本篇博文笔记于 2022-01-18 13:31:36 首发,最晚于 2022-01-20 07:58:12 修改。 罗马数字转换器(用罗马数字构造元素的值取模实现) ( 4123 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/122608526 点赞:0 踩 :0 收藏:0 打赏:0 评论:0 本篇博文笔记于 2022-01-20 19:38:12 首发,最晚于 2022-01-21 18:32:02 修改。 我的 Python.color() (Python 色彩打印控制) ( 4077 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/123194259 点赞:2 踩 :0 收藏:7 打赏:0 评论:0 本篇博文笔记于 2022-02-28 22:46:21 首发,最晚于 2022-03-03 10:30:03 修改。 练习:班里有人和我同生日难吗?(概率probability、蒙特卡洛随机模拟法) ( 3860 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/124424935 点赞:1 踩 :0 收藏:4 打赏:0 评论:0 本篇博文笔记于 2022-04-26 12:46:25 首发,最晚于 2022-04-27 21:22:07 修改。 练习:仿真模拟福彩双色球——中500w巨奖到底有多难?跑跑代码就晓得了。 ( 3656 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/125415626 点赞:3 踩 :0 收藏:6 打赏:0 评论:3 本篇博文笔记于 2022-06-22 19:54:20 首发,最晚于 2022-06-23 22:41:33 修改。 random.sample()将在python 3.9x后续版本中被弃用 ( 3599 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/120657230 点赞:0 踩 :0 收藏:0 打赏:0 评论:0 本篇博文笔记 聊天消息敏感词屏蔽系统(字符串替换 str.replace(str1, *) ) ( 3434 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/124539589 点赞:4 踩 :0 收藏:2 打赏:0 评论:3 本篇博文笔记于 2022-05-02 13:02:39 首发,最晚于 2022-05-21 06:10:42 修改。 Linux 脚本文件第一行的特殊注释符(井号和感叹号组合)的含义 ( 3410 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/123087606 点赞:0 踩 :0 收藏:4 打赏:0 评论:3 本篇博文笔记于 2022-02-23 13:08:07 首发,最晚于 2022-04-04 23:52:38 修改。 练习:小炼二维数组 ( 3270 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/125175592 点赞:9 踩 :0 收藏:5 打赏:0 评论:9 本篇博文笔记于 2022-06-07 23:54:43 首发,最晚于 2022-06-08 00:31:49 修改。 练习:求列表(整数列表)平衡点 ( 3216 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/121737612 点赞:0 踩 :0 收藏:0 打赏:0 评论:0 本篇博文笔记于 2021-12-05 23:28:10 发布。 练习:银行复利计算(用 for 循环解一道初中小题) ( 3139 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/123854548 点赞:0 踩 :0 收藏:0 打赏:0 评论:0 本篇博文笔记于 2022-03-30 20:06:37 首发,最晚于 2022-04-06 18:15:16 修改。 练习:柱状图中最大矩形 ( 3105 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/122032365 点赞:0 踩 :0 收藏:0 打赏:0 评论:0 本篇博文笔记于 2021-12-19 23:47:07 发布。 练习:电话拨号键盘的字母组合(一个缩进给我惹了麻烦) ( 3028 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/121887995 点赞:0 踩 :0 收藏:0 打赏:0 评论:0 本篇博文笔记 Python 续行符(\\)“拯救”你的超长语句 ( 3003 阅读) 博文地址:https://blog.csdn.net/m0_57158496/article/details/122878447 点赞:4 踩 :0 收藏:4 打赏:0 评论:0 本篇博文笔记于 2022-02-11 13:24:16 首发,最晚于 2022-02-11 23:41:20 修改。
推荐条件
阅读量突破三千
(更多热博 ,请点击蓝色文字跳转翻阅)
回页首
精品文章:
来源:老齐教室
回页首
◆ Python 入门指南 【Python 3.6.3】
好文力荐:
CSDN实用技巧博文: