python简单的购物车程序(含代码)

效果图:

'''
Created on 2017年9月4日

@author: len
'''


product_list = [
    ('Robot',200000),
    ('MacPro',12000),
    ('Iphone8',8888),
    ('Hello World',1200),
                ] 
shopping_list = []
user_salary=input("请输入你的工资:")
if user_salary.isdigit():
    user_salary = int(user_salary)
    while True:
        print("商品如下:")
        for index,item in enumerate(product_list): 
            
            print (index,item)
        user_choice = input("请输入要购买的商品编号:")
        if user_choice.isdigit():
            user_choice = int(user_choice)
            if user_choice < len(product_list) and user_choice > -1:
                p_item = product_list[user_choice]
                if user_salary>=p_item[1]:
                    shopping_list.append(p_item)
                    user_salary-=p_item[1]
                    print("购买商品",p_item,"成功您的余额为",user_salary,"元!" )
                else:
                    print("您的余额为",user_salary,"余额不足以购买此商品,购买失败!")
                            
            else:
                print("并无此产品!")
        elif user_choice == "q":
            print("--------shopping list-------")
            for i in shopping_list:
                print(i)
            exit()
        else:
            print("invalidate!!!")



  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值