1. 程式人生 > >python學習--購物車3

python學習--購物車3

商品列表 true use append 是否 *** 登錄 系統 操作說明

根據購物車題目的需求,本次分享的程序是將商品列表分級顯示,例如:生活用品|

|牙膏

|牙刷

|毛巾

家用電器|

|電飯鍋

即將列表存儲商品信息,改成由字典來存放,程序如下:

#初始化產品菜單
product_list = {"生活用品":[[毛巾,20],
                [洗衣液,80],
                [衣架, 10],
                [洗衣粉,60],
                [洗發水, 80],
                [廁洗劑,50]],
                "家電":[[電視機,5000],
                      [電飯煲,300],
                      [電磁爐
,200], [高壓鍋,800], [電餅鐺,200]], } current_layer = {} shopping_cart = {} shopping_cart = {} last_layer = [ product_list ] current_layer = product_list salary = 0 #登錄操作 username = input("username:") print("****************************") password = input("password:") print("****************************") i = 1 while i <= 3: if username == "maomao" and password == "123": print("welecom to shopping maket") break elif i == 3: print("input more than three times, try again after 2 hours") break else: print("you put an wrong name or password,please input again") i += 1 username = input("username:") password = input("password:") #賬號充值操作 while True: temp = input("you account is zear,please rechange money to buy:") if temp.isdigit(): salary = int(temp) break print("you input a unknow number,please check it and input again!") #購物操作 while True: print("*************** 系統操作說明 ****************") print(" q : 退出系統") print(" c : 賬號充值") print(" h : 購物記錄") print(" l : 商品列表") print("************************************************") oper = str(input(">>:")).strip() if oper == q: #判斷是否退出 exit() elif oper == c: #判斷是否充值 temp = input("please rechange money to buy:") if temp.isdigit(): salary = int(temp) elif oper == h: # 查看購物歷史記錄 f = open("product.txt", "r+", encoding="utf8") for i in f: print(i.strip()) f.close() elif oper == l: #進入購物列表 while True: if isinstance(current_layer, dict): #是否為列表 for key in current_layer: print(key) else: index = 0 for product in current_layer: print(index, product) index += 1 choice = input(">>:").strip() if len(choice) == 0:continue if choice.isdigit(): choice = int(choice) if choice >= 0 and choice < len(current_layer): product = current_layer[choice] if product[1] <= salary: if product[0] in shopping_cart: shopping_cart[product[0]][1] += 1 else: shopping_cart[product[0]] = [product[1], 1] salary -= product[1] print("\033[42;1mAdded product: %s into shoing cart ,you current balance %s \033[0m" %(product[0],str(salary))) else: print("\033[42;1m [warning] you balance is no enough\033[0m, product:" + str( product[1]) + " short for "+ str(product[1] - salary)) else: print("\033[41;1mYou choice product is no in product_list\033[0m") elif choice in current_layer: if isinstance(current_layer, dict): #是否為列表 last_layer.append(current_layer) current_layer = current_layer[choice] elif choice == "b": #返回上層商品目錄 if last_layer: current_layer = last_layer[-1] last_layer.pop() elif choice == "q": #退出 print("\033[34;1m---------------product list-----------------\033[0m") print("\033[42;1m") print("id product number pric Totalpric") index = 1 total_mon = 0 f = open("product.txt", "a+", encoding="utf8") import datetime for i in shopping_cart.keys(): total_mon += shopping_cart[i][1] * shopping_cart[i][0] print("%d %10s %7d %7d %7d" % (index, i, shopping_cart[i][1], shopping_cart[i][0], shopping_cart[i][1] * shopping_cart[i][0])) if index == 1: now_time = datetime.datetime.now().strftime([%Y-%m-%d]) f.write(now_time + "\n") # f.write(i+ "%7d"%+shopping_cart[i][0]+"\n") f.write(i + "\n") index += 1 f.close() print("you pay money is:", total_mon) print("you level money is:", salary) print("\033[0m") print("\033[34;1m-------------end-------------\033[0m") exit() elif choice == "c": #充值 temp = input("please rechange money to buy:") if temp.isdigit(): salary = int(temp) else: print("輸入內容不合法!")

python學習--購物車3