"""根据以下数据结构:goods = [{"name": "电脑", "price": 1999},{"name": "鼠标", "price": 10},{"name": "游艇", "price": 20},{"name": "美食", "price": 998},......]实现功能要求:1、启动程序后,让用户输入工资,然后进入循环,打印商品列表和编号2、允许用户根据商品编号选择商品3、用户选择商品后,检测余额是否够,够就直接扣款,并加入购物车, 不够就提醒余额不足4、可随时退出,退出时,打印已购买商品和余额"""goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美食", "price": 998}]user_input = int(input("输入工资>>>:").strip())choic = []buy_goods = []while True: for index, item in enumerate(goods, 1): print(index, item) choic.append(index) goods_num = int(input("请输入商品编号>>>:")) if goods_num in choic: if goods[goods_num - 1]['price'] < user_input: print("%s成功加入购物车!" % goods[goods_num - 1]['name']) user_input = user_input - goods[goods_num - 1]['price'] buy_goods.append(goods[goods_num - 1]['name']) print("余额:%s" % user_input) else: print("余额不足!") exit('已购商品:%s,余额:%s' % (buy_goods, user_input)) else: print("输入有误")
来源:https://www.cnblogs.com/luckgirlBlog/p/11792210.html