python-购物车程序

程序:购物车程序

需求:

启动程序后,让用户输入工资,然后打印商品列表
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额
'''


'''
while True:
    salary = int(input("请输入您的工资:"))
    info = 
    ____________________商品信息____________________
    1.IPhone 5000
    2.sanxing 4000
    3.IPad Pro 3000
    4.bluetooth 1000
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的 Python 购物车程序代码: ```python # 商品列表 products = { "apple": {"price": 5, "stock": 10}, "banana": {"price": 3, "stock": 5}, "orange": {"price": 4, "stock": 8}, "pear": {"price": 6, "stock": 6} } # 购物车 cart = {} # 打印商品列表 print("商品列表:") for name, attrs in products.items(): print(name, attrs["price"], attrs["stock"]) # 选择商品 while True: name = input("请输入要购买的商品名称:") if name in products: break else: print("商品不存在,请重新输入。") # 选择数量 while True: try: quantity = int(input("请输入要购买的数量:")) if quantity <= products[name]["stock"]: break else: print("库存不足,请重新输入。") except ValueError: print("输入无效,请输入数字。") # 添加到购物车 if name in cart: cart[name]["quantity"] += quantity else: cart[name] = {"price": products[name]["price"], "quantity": quantity} # 更新库存 products[name]["stock"] -= quantity # 打印购物车 print("购物车:") total_price = 0 for name, attrs in cart.items(): price = attrs["price"] * attrs["quantity"] total_price += price print(name, attrs["quantity"], price) # 打印总价 print("总价:", total_price) ``` 该程序实现了以下功能: 1. 打印商品列表。 2. 选择要购买的商品和数量。 3. 添加商品到购物车。 4. 更新商品库存。 5. 打印购物车和总价。 注意:该程序是一个简单的示例,实际应用中需要考虑更多因素,如商品分类、优惠活动、付款方式等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心驻彩云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值