購物系統1.0
阿新 • • 發佈:2017-05-12
enc break 存在 efault close def art default while
#!/usr/bin/python #-*- coding:utf-8 -*- import sys #讀取商品列表 goods_list = open(‘商品列表.txt‘,‘r‘,encoding="utf-8") open_goods_list = goods_list.readlines() goods_list.close() #讀取用戶名密碼 user_all = open(‘用戶列表.txt‘,‘r‘,encoding="utf-8") open_user_all = user_all.readlines() user_all.close() #提取用戶名密碼表 dic = {}for i in open_user_all: v = i.strip().split(‘|‘) dic_list = dic.setdefault(v[0],v[1]) # dic_k = dic.get() #dic_all = {} list_all = [] for qa in open_user_all: vp = qa.strip().split(‘|‘) #dic_alm = dic_all.setdefault(vp[0],[vp[1],vp[2]]) list_all.extend([vp[0],vp[1],vp[2]]) #用戶登錄 t = 0while t < 3: user_in = input("請輸入用戶名:") pwd_in = input("請輸入密碼:") if user_in in dic.keys(): i_pwd = dic.get(user_in) if i_pwd == pwd_in: print(‘歡迎登錄購物系統!‘) user_status = ‘1‘ break else: print(‘密碼不正確,請重新輸入:‘) t+= 1 else: print(‘用戶不存在,請重新輸入:‘) t += 1 if user_status == ‘1‘: x = 0 while x < 50: user_doo = input(‘請進行操作:1.查看用戶余額,2.查看商品列表,3.查看購物記錄,4.退出購物系統‘) user_do = int(user_doo) # 查看用戶余額 dic_money = {} for op in open_user_all: v_money = op.strip().split(‘|‘) dic_m = dic_money.setdefault(v_money[0],v_money[2]) i_money = dic_money.get(user_in) if user_do == 1: print(‘您當前的賬戶余額為:‘,i_money) x += 1 #查看商品列表 elif user_do == 2: # while x < 50: dic_goods = {} # p = int(input(‘請輸入頁碼,返回上一級請輸入q‘)) # if p == q: # x = 50 # else: # start = (p - 1) * 10 # end = p * 10 # int(start) # int(end) # pate_open_goods_list = open_goods_list[start:end] # print(pate_open_goods_list) for pd in open_goods_list: v_goods = pd.strip().split(‘|‘) dic_g = dic_goods.setdefault(v_goods[0], v_goods[1]) # v1 = v_goods[start:end] # # for i in v_list: # print(v1) print(dic_goods) user_buy = input("請輸入要購買的商品,返回上一級請輸入q") i_goods = dic_goods.get(user_buy) if user_buy == ‘q‘: x += 1 else: i_now_money = int(i_money) - int(i_goods) if i_now_money < 0: print(‘購買失敗!您當前的余額不足‘) x += 1 else: print(‘購買成功!您當前的余額為‘,i_now_money) goods_hist = [user_buy] f =open(‘購物記錄.txt‘,‘w‘,encoding="utf-8") f.writelines(goods_hist) f.close() #dic_fin = dic_all.update({user_in:[pwd_in,i_now_money]}) vpj = list_all.index(user_in) #vpj = ‘|‘.join(dic_all) vpjj = int(vpj) + 2 list_all[vpjj] = i_now_money # list_all_change = "|".join(‘%s‘ %id for id in list_all) # f_money = open(‘用戶列表.txt‘, ‘w‘, encoding="utf-8") # f_money.writelines(list_all_change) # f_money.close() #查看購物記錄 # 讀取購物記錄 elif user_do == 3: goods_history = open(‘購物記錄.txt‘,‘r‘, encoding="utf-8") open_goods_history = goods_history.readlines() goods_history.close() print(open_goods_history) #退出購物系統 elif user_do == 4: sys.exit(‘歡迎您下次光臨!‘)
購物系統1.0