当前位置:网站首页>练习副“产品”:自制七彩提示字符串展示工具(for循环、if条件判断)
练习副“产品”:自制七彩提示字符串展示工具(for循环、if条件判断)
2022-06-30 00:39:00 【梦幻精灵_cq】
Python 官网:https://www.python.org/
Free:大咖免费“圣经”教程《 python 完全自学教程》,不仅仅是基础那么简单……
自学并不是什么神秘的东西,一个人一辈子自学的时间总是比在学校学习的时间长,没有老师的时候总是比有老师的时候多。
—— 华罗庚

做了练习的时候,本想做个加载进度条,结果没成,却做成了这个玩意儿。
代码运行效果
代码运行视频效果动态链接:http://t.csdn.cn/sV20r

当前景色“随机”到背景色相同时,就造成了“消隐”的假相。python 033颜色输出控制(点击蓝色文字跳转查看详情)有黑、白、红、绿、青、蓝、紫七色,取消背景色“随机”,即可消除此“bug”。我的自制七彩打印工具color,点击跳转查看详细用法,本文“小玩意儿”即是用color“加色”的。
七彩提示字符串展示函数代码
def show_tip(string, num, width):
'''七彩提示字符展示 string: tip字符串 n: 展示时长(n×0.4秒) width: 展示宽度(≤50)(按英文字符计算)'''
s = string
if len(s)>50:
s = a[50]
l = system('clear') # Linux清屏。
print('\n'*3) # 打印三个空行。
for i in range(num):
flag = choice(range(7)) # flag随机赋值0、1、2、3、4、5。
if flag == 0: # 红色。
print(color(s,'f_red').center(width, '~'), end='\r')
elif flag == 1: # 绿色。
print(color(s,'f_green').center(width, '~'), end='\r')
elif flag == 2: # 蓝色。
print(color(s,'f_blue').center(width, '~'), end='\r')
elif flag == 3: # 黄色。
print(color(s,'f_yellow').center(width, '~'), end='\r')
elif flag == 4: # 紫色。
print(color(s,'f_purple').center(width, '~'), end='\r')
elif flag == 5: # 灰色。
print(color(s,'f_gray').center(width, '~'), end='\r')
elif flag == 6: # 灰色。
print(color(s,'f_black').center(width, '~'), end='\r')
sleep(0.4) # 暂停num秒。
print() # 打印空行。

本练习完整源码
#!/sur/bin/nve python
# coding: utf-8
''' filename = 'color_tip.py' author = '梦幻精灵_cq' time = '2022-06-27' '''
from time import sleep # 加载time模块暂停方法。
from os import system # 加载Linux命令容器,方便用代码执行Linux命令。
from random import choice # 加载随机数模块随机选择方法。
from python_color import color # 加载自制色彩控制打印工具模块。
def show_tip(string, num, width):
'''七彩提示字符展示 string: tip字符串 n: 展示时长(n×0.4秒) width: 展示宽度(≤50)(按英文字符计算)'''
s = string
if len(s)>50:
s = a[50]
l = system('clear') # Linux清屏。
print('\n'*3) # 打印三个空行。
for i in range(num):
flag = choice(range(7)) # flag随机赋值0、1、2、3、4、5。
if flag == 0: # 红色。
print(color(s,'f_red').center(width, '~'), end='\r')
elif flag == 1: # 绿色。
print(color(s,'f_green').center(width, '~'), end='\r')
elif flag == 2: # 蓝色。
print(color(s,'f_blue').center(width, '~'), end='\r')
elif flag == 3: # 黄色。
print(color(s,'f_yellow').center(width, '~'), end='\r')
elif flag == 4: # 紫色。
print(color(s,'f_purple').center(width, '~'), end='\r')
elif flag == 5: # 灰色。
print(color(s,'f_gray').center(width, '~'), end='\r')
elif flag == 6: # 灰色。
print(color(s,'f_black').center(width, '~'), end='\r')
sleep(0.4) # 暂停num秒。
print() # 打印空行。
if __name__ == '__main__':
num = 50 # 0.4×n秒“七彩”提示字符。
s = ' 七彩提示字符串展示 ' # 设置提示字符串
width = 50 # 提示字符串显示宽度设置。
show_tip(s, num, 50) # 调用函数展示信息。

My Up and Down:
__上一篇:__ 《悉达多》:一生之书,可以时常反刍__下一篇:__ CSV文件格式——方便好用个头最小的数据传递方式
我的HOT博:
- 练习:银行复利计算(用 for 循环解一道初中小题)(1050阅读)
- pandas 数据类型之 DataFrame(1308阅读)
- 班里有人和我同生日难吗?(概率probability、蒙特卡洛随机模拟法)(2079阅读)
- Python字符串居中显示(1450阅读)
- 练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)(1638阅读)
- 用 pandas 解一道小题(1964阅读)
- 可迭代对象和四个函数(1065阅读)
- “快乐数”判断(1226阅读)
- 罗马数字转换器(构造元素取模)(1932阅读)
- Hot:罗马数字(转换器|罗生成器)(3536阅读)
- Hot:让QQ群昵称色变的代码(26207阅读)
- Hot:斐波那契数列(递归| for )(4037阅读)
- 柱状图中最大矩形(1646阅读)
- 排序数组元素的重复起止(1236阅读)
- 电话拨号键盘字母组合(1341阅读)
- 密码强度检测器(1789阅读)
- 求列表平衡点(1812阅读)
- Hot: 字符串统计(4280阅读)
- Hot:尼姆游戏(聪明版首发)(3413阅读)尼姆游戏(优化版)(978阅读)
推荐条件点阅破千

精品文章:
- 好文力荐:《python 完全自学教程》齐伟书稿免费连载
- OPP三大特性:封装中的property
- 通过内置对象理解python'
- 正则表达式
- python中“*”的作用
- Python 完全自学手册
- 海象运算符
- Python中的 `!=`与`is not`不同
- 学习编程的正确方法
来源:老齐教室
Python 入门指南【Python 3.6.3】
好文力荐:
全栈领域优质创作者——寒佬(还是国内某高校学生)好文:《非技术文—关于英语和如何正确的提问》,“英语”和“会提问”是学习的两大利器。
CSDN实用技巧博文:
边栏推荐
猜你喜欢

Mr. Hu Bo, CIO of weiduomei, a scientific innovator: digitalization is a bloodless revolution, and the correct answer lies in the field of business

Botu V16 changes the model and firmware version of PLC

阿于的彩虹桥

网络方向哪个发展更好?数据通信工程师学习路线分享

How to design test cases

简单的页面

Distributed task scheduling elasticjob demo

手写C语言之函数的嵌套调用和链式访问

面试官:大量请求 Redis 不存在的数据,从而影响数据库,该如何解决?

MySQL高级篇1
随机推荐
[PHP] PHP pressure test, error reporting: generally, each socket address (Protocol / network address / port) is only allowed to be used
Le module twincat 3 el7211 contrôle les servocommandes baffle
About the case of SQL statements
字节、字、双字 关系
ML:置信区间的简介(精密度/准确度/精确度的三者区别及其关系)、使用方法、案例应用之详细攻略
leetcode-1. Sum of two numbers
如何在IDEA中自定义模板、快速生成完整的代码?
Mr. Hu Bo, CIO of weiduomei, a scientific innovator: digitalization is a bloodless revolution, and the correct answer lies in the field of business
Intermittent sampling and forwarding interference
VIM plug in manager VIM plug installation method
【PHP】php压测,报错:通常每个套接字地址(协议/网络地址/端口)只允许使用
MySQL基础篇1
科创人·味多美CIO胡博:数字化是不流血的革命,正确答案藏在业务的田间地头
证券开户有优惠吗究竟网上开户是否安全么?
Mysql Duplicate entry ‘xxx‘ for key ‘xxx‘
MySQL高级篇1
A Si's mood swings
如何在IDEA中创建Module、以及怎样在IDEA中删除Module?
博途V16 更改PLC的型号和固件版本
Developers, why does the maturity of container technology herald the arrival of cloud native era?