购物车小程序
作者:互联网
# 1.充值 # 2. 加载中 # 3. 读取数据 # 4. 商品展示 # 5. 购买商品 import time import json rmb = 0 car = {} def chongzhi(): global rmb while True: num = input("请充值吧亲:") if num.isdecimal(): print(num) rmb = int(num) print("恭喜,充值成功,充值金额{}".format(rmb)) break else: print("充值失败,请输入一个数字") def myprocess(percent): if percent > 1: percent - 1 strvar = int(percent * 50) * "■" print("\r[%-50s]%d%%"%(strvar,percent * 100),end="") def exe_process(): recv_size = 0 total_size = 1000 while recv_size < total_size: time.sleep(0.01) recv_size += 10 percent = recv_size/total_size myprocess(percent) def loading(): print("商品正在加载中...") exe_process() print() def read_data(filename): lst = [] with open(filename,mode="r+",encoding="utf-8") as fp: for i in fp: dic = json.loads(i) lst.append(dic) return lst def show_goods(data): strvar = "商品名称".center(18) print("序号"+"strvar"+"价格") for k,v in enumerate(data,start=1): v['num'] = k print("{v[num]:<10}{v[name]:<12}{v[price]}".format(v=v)) def error(): strvar = ''' +----------------------------------------------------------------+ | 你输入的选项不存在,请重新输入! | +----------------------------------------------------------------+ ''' print(strvar) time.sleep(1) def add_car(num,data): if num not in car: car[num] = { "name":data[num-1]["name"], "price":data[num-1]["price"], "account":1 } else: car[num]["account"] += 1 def show_car(num): print("+" + "-" * 60 + "+") print("你选择的商品具体信息:") print("*-商品名称{}".format(car[num]["name"])) print("*-商品单价{}".format(car[num]["price"])) print("*-高品数量{}".format(car[num]["account"])) print("已经成功加入到购物车~请继续购物~") print('请输入n结算!') print("+" + "-" * 60 + "+") def jiesuan(): total = 0 print("+------------------- 你购物车中的商品信息如下: ---------------------+") for k,v in car.items(): v["num"] = k v["mysum"] = v["price"] * v["account"] total += v["mysum"] print("序号:{v[num]} 商品名称:{v[name]} 商品单价:{v[price]} 此商品总价:{v[mysum]}".format(v=v)) return total def success(total,rmb): print("正在结算商品中...") exe_process() print("请稍后...") print("[一共{}元]".format(total)) print("你已经成功购买以上商品,全额还剩{},欢迎下次光临".format(rmb-total)) time.sleep(1) def del_goods(total,rmb): print("余额不足,还差{},请忍痛割爱,删除一些商品吧,".format(total-rmb)) num = input("+-------------------------------- 请输入你要删除的商品序号 ---------------------------------+") if num.isdecimal(): num = int(num) if num in car: car[num]["account"] -= 1 if not car[num]["account"]: car.pop(num) else: error() else: error() def myexit(): print("+--------------------------- 欢迎下次光临 -----------------------------+") time.sleep(1) def main(): chongzhi() loading() data = read_data("shopping_data.json") show_goods(data) sign = True while sign: num = input("请输入你要购买的商品序号:") if num.isdecimal(): num = int(num) if 1 <= num <= len(data): add_car(num,data) show_car(num) else: error() elif num.upper() == "N": while True: total = jiesuan() print(total) if total > rmb: del_goods(total, rmb) else: success(total, rmb) sign = False break elif num.upper() == "Q": myexit() sign = False else: error() main()
shopping_data.json 文件
{"name":"电脑","price":2999} {"name":"鼠标","price":30} {"name":"手机","price":1566} {"name":"键盘","price":998} {"name":"奶茶","price":10}
标签:percent,程序,购物车,rmb,num,print,def,size 来源: https://www.cnblogs.com/xo1990/p/15818000.html