标签:ber 电视机 encoding app 家电 git into strip() 初始化
根据购物车题目的需求,本次分享的程序是将商品列表分级显示,例如:生活用品|
|牙膏
|牙刷
|毛巾
家用电器|
|电饭锅
即将列表存储商品信息,改成由字典来存放,程序如下:
#初始化产品菜单 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("输入内容不合法!")
标签:ber 电视机 encoding app 家电 git into strip() 初始化
原文地址:http://www.cnblogs.com/maoxiong1985/p/7881325.html