Python入门习题4.文本进度条
2024-09-01 14:46:30
例4.1.设置一组文本进度条,使之运行效果如下:
--------执行开始--------
% 0 [->**********]
%10 [*->*********]
%20 [**->********]
%30 [***->*******]
%40 [****->******]
%50 [*****->*****]
%60 [******->****]
%70 [*******->***]
%80 [********->**]
%90 [*********->*]
%100[**********->]
--------执行结束--------
#文本进度条1.py
import time
print("{0:-^20}".format('执行开始'))
scale = 10
for i in range(scale+1):
a = '*' * i
b = '*' * (scale - i)
c = (i/scale)*100
print("%{:^3.0f}[{}->{}]".format(c,a,b))
time.sleep(0.5)
print("{0:-^20}".format('执行结束'))
在此讲解time库的使用:
获取时间:time() 得到时间戳;
ctime()得到时间的字符串表达形式;
gmtime()得到时间的程序表达形式。
时间格式化:strftime(tpl ,ts ) 其中tpl是格式化模板字符串,ts是计算机内部时间类型变量
如>>>t = time.gmtime()
time.strftime("%Y-%m-%d %H:%M:%S",t)
strptime(str,tpl)
程序计时:perf_counter() 返回精确的时间计数值。
sleep(s)休眠时间,s为秒
例4.2在命令提示符窗口实现单行动态刷新,进度条运行效果如下
100%>>>
>>> import time
>>> for i in range(101):
... print("\r{:2}%".format(i),end="")
... time.sleep(0.05)
...
例4.3实现带刷新的文本进度条
#TextProBarV3.py
import time
scale = 50
print("执行开始".center(scale//2,"-"))
start = time.perf_counter()
for i in range(scale + 1):
a = "*" * i
b = '.' * (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"+"执行结束".center(scale//2,"-"))
-----------执行开始----------
100%[**************************************************->]5.03s
-----------执行结束----------
博客参考《Python程序设计基础》高等教育出版社(第2版) 嵩天,礼欣,黄天羽著
推荐与上述教材配套的北京理工大学 嵩天,礼欣,黄天羽老师的MOOC
最新文章
- DIOCP之编写第一个应用程序(二)
- Linux SSH,FTP服务配置
- HP 7440老机器重启
- each的用法
- bzoj4199
- Getting Started with Zend Framework MVC Applications
- C#利用NPOI生成具有精确列宽行高的Excel文件
- QFII
- POJ2406 KMP算法
- DataUml Design 教程6-DataUML Design 1.1版本号正式公布(支持PD数据模型)
- 关于Oracle将小于1的数字to_char后丢掉0的解决办法
- 同步 异步 AJAX JS
- jquery 点击事件切换样式
- 报错:Maximum call stack size exceeded
- Spring中的@Transactional(rollbackFor = Exception.class)属性详解
- hibernate入门程序
- C#中,重新排列panel中的按钮
- 019_nginx upstream中keepalive参数
- PID控制器(比例-积分-微分控制器)- III
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B