使用方法:把每个案例分别执行下,看它的运行结果,再试着根据括号提示编写程序代码
- 《python 案例(一)》里不包含类 eg 1~17
- 《python 案例(二)》里有类的练习 eg 18~23
- 《python 案例(三)》关于文件操作 eg 24~26
题目:
- 设置字体颜色 ‘\033[x:xm xxxxxxxx\033[0m’
- 把数字转为二、八、十六进制数
- 判断支付密码是否合法(只能是数字) .isdigit()
- 循环输出26个字母对应ascii
- 模拟账户登录
- 猜数游戏:我心中有个1~100的数 random.randint
- 千年危机:根据年份后两位,补齐年份(4位)(列表) enumerate
- 进货 出货(列表) .find()
- 逆序遍历列表
- 查询动漫分类(列表) zip dict.get()
- 查询车次信息(字典+列表) 遍历
- 点餐(元组) enumerate()
- 打印榜单(元组) enumerate()
- 模拟手机通讯录 (集合)
- 统计字符串出现个数(字符、字符串)
- 字符串格式化输出 (字符串拼接)
- 能否构成三角形 (函数中异常处理) raise Exception
- 圆的面积与周长(类)(格式化字符串)
- 定义学生类,录入5个学生信息,存到列表中(类)(字符串劈分)
- 乐手弹奏乐器 (类)(多态)
- 买票系统 (模块:pretttable)
- 引子 (字符串) .stript() (模块:datetime)
- 推算时间 (模块:datetime)
- 引子 (模块time)
- 记录用户登录日志
- 模拟tb客服自动回复
# ============ 24、引子 (模块time)
'''
import time
time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
print(time.time())
# 1628988469.6359885
print(time.localtime(time.time()))
# time.struct_time(tm_year=2021, tm_mon=8, tm_mday=15, tm_hour=8, tm_min=48, tm_sec=22, tm_wday=6, tm_yday=227, tm_isdst=0)
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())))
# 2021-08-15 08:49:10
'''
# ============ 25、记录用户登录日志
'''
import time
log = 'log.txt'
# 记录日志
def write_logininfo(username):
with open(log,'a') as afile:
s = f'用户名{username},登陆时间{time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))}'
afile.write(s)
afile.write('\n')
# 查看日志
def read_logininfo():
with open(log,'r') as rfile:
while True:
line = rfile.readline() # 一行行地读
if line == '':
break
else:
print(line,end='')
if __name__ == '__main__':
username = input('请输入姓名(admin):')
pwd = input('请输入密码(admin):')
if username!='admin' or pwd !='admin':
print('用户名或密码不正确!')
else:
print('登陆成功')
write_logininfo(username) # 记录日志
while True:
choice = int(input('提示:0退出,1查看登录日志'))
if choice==0:
print('已退出')
break
elif choice==1:
print('登录日志如下:')
read_logininfo()
continue
else:
print('输入有误,重输!')
continue
'''
# ============ 26、模拟tb客服自动回复
'''
tbfile = 'autoreply.txt'
def find_answer(ques):
with open(tbfile,'r',encoding='utf-8') as rfile:
while True: # 因为有好几行,所以循环读
line = rfile.readline()
if not line: # 到了文件末尾 即 if line == ''
break
line_lst = line.split('|')
q = line_lst[0]
reply = line_lst[1]
if q in ques:
return reply
return '' # 如果没找到
if ques.find('订单') == -1:
# 2,在txt中找到对应回复
pass
return reply
if __name__ == '__main__':
print('您可以向我咨询:订单|物流|账户|支付|没事')
while True:
ques = input()
if ques == '没事':
print('那么886!')
break
reply = find_answer(ques) # 找问题答案,返回字符串
if not reply: # 如果没找到答案
print('你说嘛呢?问这些!订单|物流|账户|支付|没事')
else:
print(reply)
print('还有啥问题快说!订单|物流|账户|支付|没事')
'''