#2017-05-05 22:10:10一、用户登录及失败锁定账户
============================================1.文件读写2.列表split方法3.while循环
============================================
###############################datafile文件内容#larry|123123|1#linzj|123123|0###############################!/usr/bin/env python# -*- coding:utf-8 -*- # 打开文件读取用户信息f1=open(‘datafile‘,‘r‘)data=f1.read()f1.close() # 用户信息转载入用户信息列表user_info_list=[] # 初始化user_info_listuser_str_list=data.split(‘\n‘)# print(user_str_list)for item in user_str_list: temp=item.split(‘|‘) v={ ‘name‘:temp[0], ‘pwd‘:temp[1], ‘times‘:temp[2] } if int(v[‘times‘])!=3: # 将非锁定账户登录信息重置 v[‘times‘] = 0 user_info_list.append(v)print(user_info_list) # 登录验证i=0j=1while j==1: v_user_name = input(‘user name:‘) v_pwd = input(‘password:‘) for item in user_info_list: if item[‘name‘] == v_user_name and int(item[‘times‘])<3: if item[‘pwd‘] == v_pwd: j=2 # 登录成功,设置循环出口 print(‘welcome back ‘+v_user_name) else: i = int(item[‘times‘]) i += 1 item[‘times‘] = i print(item) print(user_info_list) continue elif item[‘name‘] == v_user_name and int(item[‘times‘])>=3: print(v_user_name+‘ is locked‘) else: continueprint(user_info_list) # 将用户登录的记录信息赋值给target变量k=0target=‘‘for item in user_info_list: print(item[‘name‘]) if target!=‘‘: target=target+‘\n‘ #target.append(item[‘name‘]+‘|‘+item[‘pwd‘]+‘|‘+str(item[‘times‘])) target=target+item[‘name‘]+‘|‘+item[‘pwd‘]+‘|‘+str(item[‘times‘]) print(target) # 将target变量值写入文件f2=open(‘datafile‘,‘w‘)f2.write(target)f2.close() 二、元素分类
============================================1.列表append方法2.for循环3.if流程控制
============================================
element_list=[11,22,33,44,55,66,77,88,99]
k1=[]k2=[]for item in element_list: #print(item) if item<66: k1.append(item) else: k2.append(item) print(k1,k2)v_dict={‘k1‘:k1,‘k2‘:k2}print(v_dict) 三、购物结算及充值
============================================ 1.文件的读写操作2.列表的循环遍历3.整形与字符串数据类型的相互转换 ============================================
#!/usr/src/python# -*- coding:utf-8 -*- import re# 商品初始化v_goods_list=[ {‘name‘:‘电脑‘,‘price‘:‘1000‘}, {‘name‘:‘鼠标‘,‘price‘:‘50‘}, {‘name‘:‘键盘‘,‘price‘:‘100‘}, {‘name‘:‘臭袜子‘,‘price‘:‘2‘},] # 列出商品for item in v_goods_list: print(item)#print(v_goods_list) # 显示余额f1=open(‘balance‘,‘r‘)balance=f1.read()f1.close() # 选择货物v_aws=1v_total=0while v_aws==1: if v_aws!=1: break v_goods=input(‘选择商品编号:‘) v_goods=int(v_goods) v_num=input(‘商品数量:‘) if v_goods==3: print(‘艾玛买了‘+v_num+‘双‘+v_goods_list[v_goods][‘name‘]) v_num=int(v_num) #加入购物车 if v_goods<len(v_goods_list): v_total+=int(v_goods_list[v_goods][‘price‘])*v_num print(v_total) else: print(‘编号选择错误!‘) v_aws=input(‘结算?‘) v_aws = int(v_aws) # 结算j=1while j==1: if v_total>int(balance): print(‘余额不足,请充值‘) v_balance = input(‘充值金额:‘) print(v_balance) balance = int(v_balance) + int(balance) print(balance) elif v_total==0: pass else: balance=balance-v_total print(‘购买成功‘) j=0 # 余额写回文件保存f2=open(‘balance‘,‘w‘)f2.write(str(balance))f2.close()
四、三级联动
============================================字典、列表的相互嵌套for循环遍历============================================#!/usr/src/python# -*- coding:utf-8 -*- dic={ ‘河北‘:{ ‘石家庄‘:["鹿泉", "藁城", "元氏"], ‘邯郸‘:["永年", "涉县", "磁县"]}, ‘山东‘:{ ‘济南‘:["高阳","趵突泉","泰安"], ‘聊城‘:["青岛","亳州","乾县"]}, ‘贵州‘:{ ‘遵义‘:["遵县","桐梓","道真"], ‘毕节‘:["纳雍","威宁","黔西"] }}for item in dic.keys(): print(item)v_fir_region=input(‘请选择省份‘)for item in dic[v_fir_region]: print(item)v_sec_region=input(‘请选择地市‘)for item in dic[v_fir_region][v_sec_region]: print(item)
时间: 2024-10-13 18:55:49