编程练习-购物车程序开发
根据以下数据结构:
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},
{"name": '面', "price": 9}]
salary = int(input("请输入你的工资:"))
balance = salary
shopping_cart =[]
for n, good in enumerate(goods):
n += 1
print(n, good)
while True:
select_goodnum = int(input("请输入商品对应的编号:"))
if select_goodnum == 0:
print("余额:%d,购买商品:%s"%(balance,shopping_cart))
break
else:
if select_goodnum <= 5:
select_good = goods[select_goodnum-1]
print("选择货物%s" % select_good)
shopping_cart.append(select_good)
balance = balance - select_good['price']
if balance < 0:
print("余额不足")
else:
print("无此货物,请重新输入")