product_list =[
['iphone', 5800],
['mac', 9000],
['coffee',30],
['book', 80],
['bike',1500],
['clothes',400]
]
cart = []
salary = 20000
while True:
print('商品列表:')
for i,v in enumerate(product_list):
print(i+1,v)
buy_item = input('请选择商品编号[按q退出]:')
if buy_item.isdigit():
buy_item = int(buy_item)
if buy_item < len(product_list):
if salary >= product_list[buy_item-1][1]:
cart.append(product_list[buy_item-1])
salary -= product_list[buy_item-1][1]
print('已购买:')
for i in cart:
print(i)
print('余额:',salary)
else:
print('余额不足,还剩%s' %salary)
elif buy_item == 'q':
print('您已购买以下商品:')
for i in cart:
print(i)
print('余额',salary)
break
else:
print('重新选择')
购物车作业 - 列表
最新推荐文章于 2022-09-28 22:14:01 发布