购物车
一、程序:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 search_data = [] 5 data_list =[] 6 #读取文件内容后存储数据用 7 new_data = [] 8 #写入文件时存储数据的列表 9 data_new_list=[] 10 #生成登录时字典需要用到的列表 11 yes_no = "" 12 #判断是否需要继续操作 13 good_list=[] 14 #存储购买的商品 15 dis_list=[] 16 #存储用来显示的商品列表 17 goods = [] 18 #生成商品字典时需要用到的列表 19 ################################定义空列表用来做数据转换########################################## 20 f = open('db','r',encoding="utf-8") 21 data_r=f.read() 22 f.close() 23 ###################################打开登录时的文件,并读取文件的内容到data_r####################### 24 data=data_r.strip() 25 #去除data_r里面包含的空格 26 data_list = data.split('\n') 27 #去除里面的回车符 28 for i in data_list: 29 n=i.split('|') 30 v={ 31 "name":n[0], 32 "pwd":n[1], 33 "times":n[2], 34 "coin":n[3], 35 "shoplist":n[4] 36 } 37 data_new_list.append(v) 38 #print(data_new_list) 39 #####################遍历列表里面的数据,并生成登录的字典数据,追加到新的列表中.######################## 40 41 f = open('shoping_list.txt','r',encoding="utf-8") 42 data_r=f.read() 43 f.close() 44 data = data_r.strip() 45 data_list = data.split('\n') 46 #去除里面的回车符 47 for i in data_list: 48 n=i.split(':') 49 v={ 50 "name":n[0], 51 "price":n[1] 52 } 53 goods.append(v) 54 ##########################生成商品的字典数据,追加到新的列表中.################################# 55 def shopings(): 56 for i in data_new_list: 57 if user_name == i["name"]: 58 asset = int(i["coin"]) 59 print("您的总资产为:%d" % asset) 60 page_count = len(goods)//5 61 if 0 < len(goods)%5: 62 page_count = page_count + 1 63 print("一共有%d页商品:" % page_count) 64 while True: 65 dis_page = input("请输入页码,查看商品:").strip() 66 if dis_page.isdigit(): 67 if 0 < int(dis_page) <= page_count: 68 pass 69 else: 70 continue 71 else: 72 continue 73 dis_start = (int(dis_page)-1) * 5 74 if int(dis_page) < page_count: 75 dis_end = int(dis_page) * 5 76 else: 77 dis_end = len(goods) 78 dis_list = goods[dis_start:dis_end] 79 for num,i in enumerate(dis_list,dis_start): 80 print(num,i["name"].ljust(8),"\t",i["price"].ljust(6)) 81 good_num = input("请输入商品序号,来选择商品购买:").strip() 82 try: 83 num = int(good_num) 84 except: 85 print ("Error:请输入数字.") 86 else: 87 if 0 <= num < dis_end: 88 print("您选择的商品为:%s" % goods[num]["name"]) 89 good_price = int(goods[num]["price"]) 90 if asset > good_price: 91 asset = asset-good_price 92 93 good_list.append(goods[num]["name"]) 94 95 print("您成功购买的商品为:%s" % good_list) 96 print ("您现在总资产为:%d" % asset) 97 yes_no=str(input("如需继续购买请输入yes|y,否则请输入其他进行退出:\t").strip()) 98 if yes_no =="yes" or yes_no=="Yes" or yes_no=="Y" or yes_no=="y": 99 pass 100 else: 101 break 102 else: 103 print ("账户余额不足!") 104 break 105 else: 106 print("请输入商品范围内的序号!") 107 for i in data_new_list: 108 if user_name == i["name"]: 109 i["coin"] = str(asset) 110 print(i["coin"]) 111 for j in good_list: 112 i["shoplist"] = i["shoplist"] + j + "," 113 ########################################## 定义购买商品的函数,以便调用!######################### 114 a = 0 115 # 116 #定义一个标志变量进行判断 117 while True: 118 l_name=input("请输入用户名:").strip()#输入要登录的用户名 119 for r_key in data_new_list: #遍历列表,逐条读取数据 120 times_num=int(r_key["times"]) 121 if l_name == r_key["name"]:#判断用户是否存在 122 a = 1 123 if times_num > 3:#若用户存在,判断登录失败次数是否超过3次 124 print("输入的密码超过三次已经被锁定,请联系管理员进") 125 break 126 l_pwd = str(input("请输入密码:")).strip() 127 if l_pwd == r_key["pwd"]: 128 print("欢迎 %s 登录!" % l_name) 129 r_key["times"]= '0'#登录成功后给登录密码失败次数清零 130 a = 2 131 user_name = r_key["name"] 132 else: 133 print("密码错误,请重新输入!") 134 times_num += 1 135 r_key["times"]=str(times_num) 136 if times_num > 3: 137 print("输入的密码超过三次已经被锁定,请联系管理员进行解锁!") 138 break 139 if a == 0: 140 print ("用户不存在!") 141 print("请重新登录!") 142 elif a == 2: 143 shopings() 144 print(good_list) 145 go_on = input("继续请输入Y|y|yes,否则请输入其他:").strip() 146 if go_on == "Y" or go_on == "y" or go_on == "yes": 147 pass 148 else: 149 break 150 #进行登录并判断用户是否存在,密码是否正确,登录密码失败次数超过三次是否被锁定 151 while True: 152 n = input("是否要进管理模式(y|yes),否则请输入其他:").strip() 153 if n == "y" or n == "yes": 154 super_n = input("请输入管理员帐号:").strip() 155 if super_n == "superadmin": 156 super_pwd = input("请输入管理员密码:").strip() 157 if super_pwd == "1qaz": 158 user_name = input("请输入需要重置的用户名:").strip() 159 for i in data_new_list: 160 if i["name"]== user_name: 161 i["times"] = "0" 162 print("操作成功,请退出!") 163 break 164 else: 165 print("没有该用户!") 166 else: 167 print("密码错误!") 168 else: 169 print("账号错误!") 170 else: 171 break 172 #管理员进行重置密码次数 173 for i in data_new_list: 174 new_data.append(i["name"]+"|"+i["pwd"]+"|"+i["times"]+"|"+i["coin"]+"|"+i["shoplist"]) 175 #遍历data_new_list列表中的数据,生成新的列表并加"|"号 176 print (new_data) 177 f = open('db','w',encoding="utf-8") 178 for i in new_data: 179 print(i) 180 f.write(i) 181 f.write("\n") 182 f.close() 183 #逐条写入文件并关闭 184 f = open("db", "r", encoding="utf-8") 185 search_r = f.read() 186 f.close() 187 while True: 188 dis_search = input("是否要查询购物车请输入Y|y|yes,否则请输入其他:").strip() 189 if dis_search == "Y" or dis_search == "y" or dis_search == "yes": 190 search=search_r.strip() 191 #去除data_r里面包含的空格 192 search_list = search.split('\n') 193 #去除里面的回车符 194 for i in search_list: 195 n=i.split('|') 196 v={ 197 "name":n[0], 198 "pwd":n[1], 199 "times":n[2], 200 "coin":n[3], 201 "shoplist":n[4] 202 } 203 search_data.append(v) 204 search_name = input("请输入名字:") 205 for i in search_data: 206 if search_name == i["name"]: 207 print(i["shoplist"]) 208 else: 209 pass 210 else: 211 break 212 #################################查询商品列表###########
二、数据文件
登录:
db
alex|123123|0|5000|
eric|123456|0|5000|
~
商品:
shoping_list.txt
电脑:1999 鼠标:10 游艇:20 美女:998 MAC:800 键盘:50 显示器:1000 耳机:70 汽车:1500 自行车:100 办公桌:150 苹果:5 香蕉:6 桔子:7 椰子:8

浙公网安备 33010602011771号