python实现购物车买商品不超过10000元的多种商品组合_Python购物车实现

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('大哥,您输入有误吧')

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值