goods = [{"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998},]def Product_Display(): ''' 商品展示 :return: ''' for i in range(len(goods)): print(i + 1, goods[i]['name'], goods[i]['price'])def Account_recharge(): ''' 账户充值 :return: ''' global Money Recharge = input('请输入要充值的金额:') if Recharge.isdecimal() > 0 and int(Recharge): Money = Money + int(Recharge) print(f'恭喜您成功充值{Recharge},账户余额{Money}') print('*' * 27) else: print('很抱歉,您输入的金额有误!')def Shopping_Cart_Delete_Items(): ''' 购物车删除商品 :return: ''' if Shopping_Cart: while True: for num,res in enumerate(Shopping_Cart,1): #展示购物车排列(1,(电脑:1)) print(f'序号:{num},商品名称:{res},数量:{Shopping_Cart[res]},总计:{Shopping_Cart[res]*Shopping_Cart_Price[res]}') Delete_The_Goods = input('请输入您要删除的商品序号') if Delete_The_Goods.isdecimal() and 0 < int(Delete_The_Goods) <= len(Shopping_Cart): Shopping_Cart[dict(enumerate(Shopping_Cart,1))[int(Delete_The_Goods)]] = Shopping_Cart.setdefault(dict(enumerate(Shopping_Cart,1))[int(Delete_The_Goods)],0) - 1 print(f"您已成功删除一个:{dict(enumerate(Shopping_Cart, 1))[int(Delete_The_Goods)]}") print('*' * 27) if Shopping_Cart[dict(enumerate(Shopping_Cart,1))[int(Delete_The_Goods)]] == 0: print(f"您选择的该商品:{dict(enumerate(Shopping_Cart,1))[int(Delete_The_Goods)]}已清除") del Shopping_Cart[dict(enumerate(Shopping_Cart,1))[int(Delete_The_Goods)]] print('*' * 27) break else: print('您输入的商品序号不存在!') print('*' * 27) break else: print('您未选择商品,请先购物!') print('*' * 27)def Shopping_Cart_Display(): ''' 购物车展示商品 :return: ''' if Shopping_Cart: for num, res in enumerate(Shopping_Cart, 1): print(f'序号:{num},商品名称:{res},数量:{Shopping_Cart[res]},总计:{Shopping_Cart[res] * Shopping_Cart_Price[res]}') print('*' * 27) else: print('您未选择商品,请先购物!') print('*' * 27)def Shopping_Cart_Settlement(): ''' 购物车结算 :return: ''' global Money Money_js = 0 if Shopping_Cart: for num, res in enumerate(Shopping_Cart, 1): Money_js += Shopping_Cart[res] * Shopping_Cart_Price[res] if Money >= Money_js: print(f"恭喜您结算成功,账户剩余余额{Money - Money_js}") print('*' * 27) else: print(f'您的账户余额剩余{Money}请充值!') print('*' * 27) else: print('您未选择商品,请先购物!')def Exit(): global Money_js if Shopping_Cart: Money_js = 0 for num, res in enumerate(Shopping_Cart, 1): Money_js += Shopping_Cart[res] * Shopping_Cart_Price[res] if Money > 0: print('以下是您已购买的商品'.center(27)) print(f'您一共消费{Money_js}'.center(27)) for num, res in enumerate(Shopping_Cart, 1): print(f'商品名称:{res},数量:{Shopping_Cart[res]},总计:{Shopping_Cart[res] * Shopping_Cart_Price[res]}') exit() else: print('欢迎您下次光临'.center(27)) print('*'*27) exit() else: print('欢迎您下次光临!') exit()dic = { 5:Account_recharge, #账户充值 6:Shopping_Cart_Delete_Items, #购物车删除商品 7:Shopping_Cart_Display, #购物车展示商品 8:Shopping_Cart_Settlement, #购物车结算 9:Exit #退出}Shopping_Cart_Price = {}for i in range(len(goods)): Shopping_Cart_Price[goods[i]['name']] = goods[i]['price']Shopping_Cart = {}Money = 0while True: Product_Display() User_Input = input('5.账户充值\n6.购物车删除商品\n7.购物车展示商品\n8.购物车结算\n9.退出程序\n请输入1~9:') if User_Input.isdecimal() and int(User_Input) and 0 < int(User_Input) < 10: if 0 < int(User_Input) < len(goods)+1: User_Input = int(User_Input) - 1 Shopping_Cart[goods[User_Input]['name']] = Shopping_Cart.get(goods[User_Input]['name'],0) + 1 print(f"已经将您选择的\"{goods[User_Input]['name']}\"加入购物车") print('•'*27) elif len(dic) - 1 < int(User_Input) < 10: dic[int(User_Input)]() else: print('输入有误,重新输入!')
来源:https://www.cnblogs.com/remixnameless/p/11994919.html