python 改良購物車
阿新 • • 發佈:2018-06-07
imp OS 3-9 odin div Coding 商品列表 成功 pan
Python購物車
一、流程圖
二、目錄結構
三、代碼
1、bin目錄:
1 import sys,os,json 2 dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 3 sys.path.append(dir_path) 4 from core import shopping 5 6 if __name__ == "__main__": 7 shopping.main()main.py
2、conf目錄:
未編寫
3、core目錄:
import json,sys,os dir_pathlogin.py= os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(dir_path) user_path = dir_path + "/db/user_data.json" user_infor_path = dir_path + "/db/user_infor.json" with open(user_path, "r", encoding="UTF-8") as f_load: user_list = json.load(f_load) with open(user_infor_path, "r", encoding="UTF-8") as f_load: user_infor_list = json.load(f_load) def login_check(): name = input("input your user_name:") passwd = input("input your user_passwd:") if name in user_list and passwd == user_list[name]: print("登陸成功!") return name else: print("wrong uesr_name or password...") exit() def creat_user(): new_user = input("input user_name:") if new_user in user_list: print("用戶已存在") return else: new_pswd = input("input user_pswd:") user_list[new_user] = new_pswd with open(user_path, "w", encoding="UTF-8") as f_dump: json.dump(user_list, f_dump) user_infor_list[new_user] = {"money": 1000, "user_goods": []} with open(user_infor_path, "w", encoding="UTF-8") as f_dump: json.dump(user_infor_list, f_dump) print("用戶創建成功!")
1 import sys,os,json 2 dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 3 sys.path.append(dir_path) 4 from core import login 5 user_path = dir_path + "/db/user_infor.json" 6 goods_path = dir_path + "/db/goods.json" 7 8 def buy(who): 9 with open(user_path, "r", encoding="UTF-8") as f_load: 10 user_infor = json.load(f_load) 11 user_goods_1 = user_infor[who] 12 user_goods = user_goods_1["user_goods"] 13 while True: 14 print("--商品列表--") 15 shop_list = goods_list() 16 for i in shop_list: 17 print(i,shop_list[i]) 18 print("------------") 19 print("賬戶余額:",user_goods_1["money"]) 20 buy_sonething = input("選擇購買的商品,按q退出!->") 21 if buy_sonething in shop_list: 22 user_money = user_goods_1["money"] 23 cost_money = shop_list[buy_sonething] 24 now_money = user_money - cost_money 25 if int(now_money) >= 0 : 26 print("余額",now_money) 27 user_goods.append([buy_sonething,shop_list[buy_sonething]]) 28 user_goods_1["user_goods"] = user_goods 29 user_goods_1["money"] = now_money 30 user_infor[who] = user_goods_1 31 with open(user_path, "w", encoding="UTF-8") as f_dump: 32 json.dump(user_infor, f_dump) 33 g = input("按任意鍵繼續,按q退出") 34 if g == ‘q‘: 35 print("購物清單:") 36 for i in user_goods: 37 print(i) 38 return now_money 39 else: 40 continue 41 else: 42 print("余額不足!您的賬戶余額為:%s"%user_money) 43 exit() 44 elif buy_sonething == "q": 45 exit() 46 else: 47 print("請輸入正確的商品名稱!") 48 49 def goods_list(): 50 with open(goods_path,"r", encoding="utf-8") as f: 51 shop_list = json.load(f) 52 return shop_list 53 54 def type_choice(): 55 choice = input("請選擇登陸模式(1.客戶/2.商家):") 56 if choice == "1": 57 print("顧客模式:") 58 return 1 59 elif choice == "2": 60 print("商家模式:") 61 return 2 62 else: 63 print("輸入錯誤!") 64 return 0 65 66 def customer_type(): 67 web = ‘‘‘ 68 "-------------" 69 "用戶登錄按:1" 70 "用戶註冊按:2" 71 "-------------" 72 ‘‘‘ 73 while True: 74 print(web) 75 i = input("--->:") 76 if i == "1": 77 user = login.login_check() 78 break 79 elif i == "2": 80 login.creat_user() 81 else: 82 print("請選擇正確的編號!") 83 buy(user) 84 85 def manage_type(): 86 password = "123456" 87 pswd = input("請輸入管理員密碼:") 88 if pswd == password: 89 while True: 90 goods_dir = goods_list() 91 for i in goods_dir: 92 print(i,goods_dir[i]) 93 goods_contral = input("1:修改價格、2:添加商品、3:刪除商品、q:退出:") 94 if goods_contral == "1": 95 change_goods = input("選擇要修改的商品:") 96 goods_money = input("輸入%s的新價格:"%change_goods) 97 goods_dir[change_goods] = int(goods_money) 98 with open(goods_path, "w", encoding="UTF-8") as f_dump: 99 json.dump(goods_dir, f_dump) 100 elif goods_contral == "2": 101 new_goods = input("新商品:") 102 new_price = input("價格:") 103 goods_dir[new_goods] = int(new_price) 104 print("添加成功") 105 with open(goods_path, "w", encoding="UTF-8") as f_dump: 106 json.dump(goods_dir, f_dump) 107 elif goods_contral == "3": 108 del_goods = input("選擇要刪除的商品:") 109 del goods_dir[del_goods] 110 with open(goods_path, "w", encoding="UTF-8") as f_dump: 111 json.dump(goods_dir, f_dump) 112 elif goods_contral == "q": 113 break 114 else: 115 print("輸入錯誤!") 116 else: 117 print("密碼錯誤!") 118 exit() 119 120 def main(): 121 user_type = type_choice() 122 if user_type == 1: # 客戶 123 customer_type() 124 elif user_type == 2: # 商家 125 manage_type() 126 else: # 錯誤 127 exit() 128 129 if __name__ == "__main__": 130 main()shopping.py
4、db目錄:
{"bike": 200, "fruit": 80, "book": 300, "phone": 2000}goods.json
{"aa": "123", "zz": "123", "jj": "123"}user_data.json
{"jj": {"money": 919, "user_goods": [["fruit", 80], ["phone", 1]]}, "zz": {"money": 379, "user_goods": [["bike", 300], ["bike", 200], ["bike", 200]]}, "aa": {"money": 1420, "user_goods": [["bike", 300], ["fruit", 80]]}}user_infor.json
5、doc目錄:
readme.doc
python 改良購物車