tenguaj 2023-02-09 11:49 采纳率: 75%
浏览 169
已结题

Python实现商场购物

Python做一个商场购物代码运行后有
登录
注册
退出
注册成功后可以登录,登录成功后会有充值,充值成功后去选择购物
查询余额
充值
查看商品,最少十个可以添加的物品
添加商品到购物车
购物车

进入购物车后购物车里面可以查看
已经选择的商品,修改商品的数量,删除商品,结算,结算后显示余额,询问手机号和地址,

  • 写回答

9条回答 默认 最新

  • 社区专家-Monster-XH 2023-02-09 11:58
    关注
    
    class User:
        def __init__(self, name, balance):
            self.name = name
            self.balance = balance
            self.cart = []
    
        def add_to_cart(self, item, price):
            self.cart.append((item, price))
    
        #移除,输入想要删除的商品id和数量
        def remove_from_cart(self, id, count):
            for i in range(count):
                self.cart.remove(self.cart[id])
    
        def check_balance(self):
            print(f"当前余额为: {self.balance}")
    
    
    class ShoppingMarket:
        def __init__(self):
            self.users = {}
            #10个商品
            self.products = {1:("苹果", 10),  2:("香蕉", 20),  3:("橘子", 30),  4:("梨", 40),  5:("葡萄", 50),  6:("西瓜", 60),  7:("芒果", 70),  8:("榴莲", 80),  9:("火龙果", 90),  10:("草莓", 100)}
    
        def register(self, name, password):
            self.users[name] = User(name, 0)
    
        def login(self, name, password):
            if name in self.users:
                self.current_user = self.users[name]
                return True
            return False
    
        def add_balance(self, amount):
            self.current_user.balance += amount
    
        def show_cart(self):
            print("购物车:")
            for item, price in self.current_user.cart:
                print(f"{item}: {price}")
    
        def show_products(self):
            print("所有商品:")
            for id, (item, price) in self.products.items():
                print(f"{id}. {item}: {price}")
    
        def buy(self, id, count, price):
            for i in range(count):
                self.current_user.add_to_cart(self.products[id][0], price)
    
        # 移除,输入想要删除的商品id和数量
        def remove_from_cart(self, id, count):
            for i in range(count):
                self.current_user.cart.remove(self.current_user.cart[id])
    
        # 结账,写地址和电话后扣除余额,余额不足则提示;结账完成后,显示购买的商品的手机号和地址,提示结账成功,清空购物车
        def checkout(self, address, phone):
            total_price = 0
            for item, price in self.current_user.cart:
                total_price += price
            if total_price > self.current_user.balance:
                print("余额不足")
            else:
                self.current_user.balance -= total_price
                print("结账成功")
                print("购买的商品为:", item)
                #余额:收货地址为:手机号:
                print("余额:", self.current_user.balance, "收货地址为:", address, "手机号:", phone)
                self.current_user.cart = []
    
        def run(self):
    
            while True:
                print("1. 注册")
                print("2. 登录")
                print("3. 退出")
                choice = input("请输入选项: ")
                if choice == "1":
                    name = input("请输入用户名: ")
                    password = input("请输入密码: ")
                    self.register(name, password)
                elif choice == "2":
                    name = input("请输入用户名: ")
                    password = input("请输入密码: ")
                    if self.login(name, password):
                        while True:
                            print("1. 查看余额")
                            print("2. 充值")
                            print("3. 查看购物车")
                            print("4. 查看所有商品")
                            print("5. 添加商品到购物车")
                            print("6. 从购物车删除商品")
                            print("7. 结账")
                            print("8. 退出")
                            choice = input("请输入选项: ")
                            if choice == "1":
                                self.current_user.check_balance()
                            elif choice == "2":
                                amount = int(input("请输入充值金额: "))
                                self.add_balance(amount)
                            elif choice == "3":
                                self.show_cart()
                            elif choice == "4":
                                self.show_products()
                            elif choice == "5":
                                id = int(input("请输入商品id: "))
                                count = int(input("请输入商品数量: "))
                                price = self.products[id][1]
                                self.buy(id, count, price)
                            elif choice == "6":
                                id = int(input("请输入移除的商品id: "))
                                count = int(input("请输入移除的商品数量: "))
                                self.remove_from_cart(id, count)
                            elif choice == "7":
                                address = input("请输入地址: ")
                                phone = input("请输入电话: ")
                                self.checkout(address, phone)
                            elif choice == "8":
                                break
                elif choice == "3":
                    break
    
    
    if __name__ == "__main__":
        market = ShoppingMarket()
        market.run()
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(8条)

报告相同问题?

问题事件

  • 系统已结题 2月17日
  • 已采纳回答 2月9日
  • 创建了问题 2月9日

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了