1 #!/usr/bin/python 2 # -*- coding:utf-8 -*- 3 #购物清单写在文件里,用户可以查询历史购买物品,用户有余额 4 5 # ####################################读取文件列表#################################### 6 # ###############读取用户信息############### 7 f1 = open('user_info','r') 8 data = f1.read() 9 f1.close() 10 11 user_info_list = []#用户信息列表 12 user_str_list = [i for i in data.split(' ') if i] # 移除data.split()生成的空元素 13 for item in user_str_list: 14 temp = item.split('|') 15 v = { 16 'username': temp[0], 17 'password': temp[1], 18 'times': temp[2], 19 'money':int(temp[3]) 20 } 21 user_info_list.append(v) 22 # print(user_info_list) 23 # ####################读取商品列表########### 24 f3 = open('goods_list','r',encoding='utf-8')#需要转为utf-8 25 data = f3.read() 26 f3.close() 27 goods_list = []#商品信息列表 28 goods_str_list = [i for i in data.split(' ') if i] # 移除data.split()生成的空元素 29 for goods_item in goods_str_list: 30 temp = goods_item.split('|') 31 v = { 32 'goods_name': temp[0], 33 'goods_price': temp[1], 34 } 35 goods_list.append(v) 36 # ###################################时间 37 import datetime 38 import time 39 now_time = time.strftime("%Y-%m-%d %X",time.localtime()) 40 # print(now_time) 41 # #######################################用户登录################################## 42 message_login = '%s欢迎您,账户余额:%d'#登录成功显示用户基本信息 43 message_totle_price="账单总金额:%d" 44 shopping_price_totle=0 45 shopping_car=[] 46 shopping_list = [] 47 history_shopping_list= [] 48 history_shopping_str='' 49 p = 1#购物列表初始页码 50 state_login = 3 51 while state_login > 0 : 52 name = input("请输入用户名:") 53 pwd = input("请输入密码:") 54 for item in user_info_list : 55 if name == item['username'] : 56 if int(item['times']) > 0 : 57 if pwd == item['password'] : 58 print(message_login %(name,item['money']))#登录成功展示个人信息 59 #主菜单 60 while True : 61 print("***********************************************") 62 user_chose = input("请选择您要的操作: 1-购物 2-查询 3-退出 ") 63 shopping_state = True 64 if user_chose == '1': 65 # print("**********************************************") 66 while True: 67 print("*******商品列表如下:*******") 68 if shopping_state==False: 69 print("购物清单如下: ") 70 print(shopping_car) 71 print(message_totle_price %(shopping_price_totle)) 72 if shopping_price_totle > int(item['money']): 73 print("余额不足,重新购买") 74 shopping_car.clear() #余额不足重新购买 75 shopping_price_totle = 0 76 shopping_state = True 77 else: 78 print("购买成功") 79 item['money']=int(item['money'])-shopping_price_totle 80 for shopping_car_item in shopping_car: 81 shopping_list.append(shopping_car_item) 82 # ##################################把用户购物记录写入文件################################## 83 shopping_line = '' 84 # i = 1 85 for item5 in shopping_list: 86 shopping_line = ' '+shopping_line + name + '|' + item5+"| "+now_time 87 # 写入文件 88 f4 = open("dodo_shopping_log", 'a',encoding='utf-8') 89 f4.write(shopping_line) 90 f4.close() 91 ######################写入余额############ 92 line = '' 93 i = 1 94 for item2 in user_info_list: 95 if i < user_info_list.__len__(): 96 line = line + item2['username'] + '|' + item2['password'] + '|' + str( 97 item2['times']) + '|' + str(item2['money']) + ' ' 98 i = i + 1 99 else: 100 line = line + item2['username'] + '|' + item2['password'] + '|' + str( 101 item2['times']) + "|" + str(item2['money']) 102 103 # 写入文件 104 f2 = open("user_info", 'w') 105 f2.write(line) 106 f2.close() 107 break 108 else: 109 #显示购物列表 110 if len(goods_list)%5 > 0 : 111 totle_page = str(int(len(goods_list) / 5) + 1) 112 else: 113 totle_page = str(int(len(goods_list) / 5) ) 114 print("商品名称 " + "|" + " 商品价格 ") 115 print("--------------------------") 116 start = (p - 1) * 5 117 end = p * 5 118 for i in goods_list[start:end]: 119 v = i['goods_name'] 120 v1 = v.ljust(9) 121 v1 = v1.replace(' ', ' ') 122 v2 = i['goods_price'] 123 # 分页显示 124 print(v1, v2) 125 print(str(p) + '/' + totle_page)# 显示当前页数 126 shopping = input(('请输入您想要购买物品的名称 没有想要商品继续浏览请按任意键 结账请按-0:')) 127 if shopping == '0': 128 shopping_state = False 129 else: 130 for items in goods_list: 131 if shopping == items['goods_name'] : 132 shopping_car.append(shopping) 133 shopping_price_totle += int(items['goods_price']) 134 break 135 else: 136 pass 137 p = int(input("请输入页码(请输入整数):")) 138 elif user_chose =='2': 139 # print(history_shopping) 140 #把所有商品拼接成一个字符串 141 # #################读取用户历史购买记录########### 142 f5 = open('dodo_shopping_log', 'r',encoding='utf-8') 143 history_data = f5.read() 144 f5.close() 145 146 history_shopping_list = [] # 用户信息列表 147 history_shopping_str = [i for i in history_data.split(' ') if i] # 移除data.split()生成的空元素 148 for read_history_item in history_shopping_str: 149 temp = read_history_item.split('|') 150 v = { 151 'log_goods': temp[1], 152 'log_time': temp[2], 153 'log_username': temp[0] 154 } 155 history_shopping_list.append(v) 156 # print(history_shopping_list) 157 history_state = input("1-查询所有记录 2-查询历史商品") 158 if history_state == '2': 159 history_goods = input("请输入您要查询的商品:") 160 for item6 in history_shopping_list : 161 # print(item6['log_username']) 162 163 if history_goods == item6['log_goods'] and name==item6['log_username']: 164 # if history_goods in history_data : 165 print("您购买过!") 166 break 167 else: 168 print('您没有买过') 169 else:#查询所有记录 170 for item7 in history_shopping_list: 171 if name==item7['log_username']: 172 print(item7['log_username']+'|'+item7['log_goods']+'|'+item7['log_time']) 173 else: 174 print("欢迎下次再来!再见") 175 break 176 state_login =0 177 item['times']=3 178 state_login = 0 179 break 180 181 else: 182 state_login -= 1 183 if state_login == 0: 184 print("尝试次数超限,登录失败") 185 else: 186 print("输入有误!,请重新输入") 187 188 item['times']=int(item['times'])-1 189 break 190 else: 191 print("用户已被锁定!") 192 state_login = 0 193 break 194 else : 195 state_login -= 1 196 if state_login == 0 : 197 print("尝试次数超限,登录失败") 198 else: 199 print("输入有误,请重新输入") 200 #print(db_list) 201 ########################################变成字符串############################## 202 line ='' 203 i = 1 204 for item2 in user_info_list : 205 if i<user_info_list.__len__(): 206 line = line+item2['username']+'|'+item2['password']+'|'+str(item2['times'])+'|'+str(item2['money'])+' ' 207 i=i+1 208 else : 209 line =line+item2['username']+'|'+item2['password']+'|'+str(item2['times'])+"|"+str(item2['money']) 210 # m ="zhi:%r" 211 # print(m %(line)) 212 #写入文件 213 f2 = open("user_info",'w') 214 f2.write(line) 215 f2.close()