salary=int(input("please input your salary:"))
product_list=[['iphone',5299],['coffee',30],['bike',299],['vivo x9',2499],['cake',40],['book',99]]
product_car={}
total_cost=0
while True:
print('--------可以购买的商品如下--------')
for number in range(len(product_list)):
product = product_list[number]
print(number,product)
print('q','quit')
choice=input('input your select product number or q').strip()
if choice.isdigit():
choice=int(choice)
if choice < len(product_list) and choice >=0:
product = product_list[choice] #获取到要购买的商品信息和价格
if salary-product[1]>=0: #判断是否买得起
salary -=product[1]
print('将商品%s加入购物车,你现在的余额是%s'%(product[0],salary))
if product[0] in product_car:
product_car[product[0]][1]+=1 #商品已在购物车,将商品数量加1
else:
product_car[product[0]]=[product[1],1] #商品未在购物车,将商品单价和数量加入购物车
print('目前购物车',product_car)
else:
print('你买该商品%s,还差%s元'%(product[0],product[1]-salary))
else:
print('没有你选择的商品')
elif choice == 'q':
print('您购买的商品信息如下')
print('id\t商品\t数量\t单价\t总价')
icount = 1
for key in product_car:
total_cost+= product_car[key][0]*product_car[key][1]
print('%s\t%s\t\t%s\t%s\t%s'%(icount,key,product_car[key][1],product_car[key][0],product_car[key][0]*product_car[key][1]))
icount+=1
print('您的总消费为%s'%total_cost)
break
else:
print('大哥,您输入有误吧')