12.输出商品列表,用户输入序号,显示用户选中的商品(升级题)
商品列表:
goods = [{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998}, ]
要求:
1:页面显示 序号 + 商品名称 + 商品价格,如:
1 电脑 1999
2 鼠标 10
…
2:用户输入选择的商品序号,然后打印商品名称及商品价格
3:如果用户输入的商品序号有误,则提示输入有误,并重新输入。
4:用户输入Q或者q,退出程序。
lst = [] shopcars = {} while 1: goods = [{"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ] for i,j in enumerate(goods,1): print(i,j[‘name‘],j[‘price‘]) lst.append(j) choice = input(‘请输入商品编号选择商品:‘) if choice.isdigit(): choice = int(choice) pname = lst[choice-1][‘name‘] pprice = lst[choice-1][‘price‘] print(pname,pprice) if pname in shopcars: shopcars[pname][‘amount‘] +=1 else: shopcars[pname] = {‘pprice‘: pprice, ‘amount‘: 1} elif choice.upper() == ‘Q‘: print(shopcars) break else: print(‘商品序号有误!‘)
原文地址:https://www.cnblogs.com/apollo1616/p/9460556.html
时间: 2024-10-25 15:19:52