Python 商场周年庆购物打折活动代码示例

本文将介绍使用 Python 编写一个模拟商场周年庆购物打折活动的程序,包含以下主要模块:

  1. 商品信息模块

    • 定义一个商品类 Product,包含商品名称 name、原价 original_price、折后价 discount_price、折扣率 discount_rate 等属性。
    • 编写计算折后价的方法 calculate_discount_price()
    • 在程序中定义一些商品对象,并保存在列表 products 中,供后续使用。
  2. 用户输入模块

    • 通过 input() 函数获取用户输入的商品编号 product_id 和购买数量 quantity
    • 对输入进行检查,确保输入合法,例如商品编号必须存在,购买数量必须为正整数。
  3. 购物车模块

    • 定义一个购物车类 ShoppingCart,包含以下方法:
      • add_item(product_id, quantity): 添加商品到购物车。
      • remove_item(product_id): 从购物车中删除商品。
      • update_quantity(product_id, quantity): 修改购物车中商品的数量。
      • calculate_total_price(): 计算购物车中所有商品的总价。
    • 使用一个列表 items 保存购物车中商品的信息,例如 [{'product_id': 1, 'quantity': 2}, {'product_id': 3, 'quantity': 1}]
    • 在用户输入商品编号和购买数量后,将对应商品加入购物车。
  4. 打折计算模块

    • 定义一个函数 calculate_discount(total_price),根据购物车中商品的总价 total_price,计算出折扣后的价格,并返回给用户。
    • 打折规则可以根据实际情况设置,例如满 100 元减 10 元,满 200 元减 20 元等。
  5. 结算模块

    • 在用户完成购物后,调用 calculate_discount() 函数计算出折扣后的价格,并输出给用户。
    • 清空购物车,准备下一次购物。
  6. 程序入口模块

    • 通过循环接受用户输入,调用购物车模块完成购物车操作,最终调用结算模块完成结算,直到用户退出程序。

代码示例

class Product:
    def __init__(self, name, original_price, discount_rate):
        self.name = name
        self.original_price = original_price
        self.discount_rate = discount_rate
        self.discount_price = self.calculate_discount_price()

    def calculate_discount_price(self):
        return self.original_price * (1 - self.discount_rate)

class ShoppingCart:
    def __init__(self):
        self.items = []

    def add_item(self, product_id, quantity):
        self.items.append({'product_id': product_id, 'quantity': quantity})

    def remove_item(self, product_id):
        for i, item in enumerate(self.items):
            if item['product_id'] == product_id:
                del self.items[i]
                break

    def update_quantity(self, product_id, quantity):
        for item in self.items:
            if item['product_id'] == product_id:
                item['quantity'] = quantity
                break

    def calculate_total_price(self):
        total_price = 0
        for item in self.items:
            product = products[item['product_id'] - 1]
            total_price += product.discount_price * item['quantity']
        return total_price

def calculate_discount(total_price):
    # 根据实际情况设置打折规则
    if total_price >= 100:
        discount = 10
    elif total_price >= 200:
        discount = 20
    else:
        discount = 0
    return total_price - discount

# 商品信息
products = [
    Product('商品1', 100, 0.1),
    Product('商品2', 200, 0.2),
    Product('商品3', 300, 0.3)
]

# 初始化购物车
cart = ShoppingCart()

# 程序入口
while True:
    print('请选择操作:')
    print('1. 添加商品')
    print('2. 删除商品')
    print('3. 修改商品数量')
    print('4. 查看购物车')
    print('5. 结算')
    print('6. 退出')

    choice = input('请输入您的选择: ')  

    if choice == '1':
        product_id = int(input('请输入商品编号: '))
        quantity = int(input('请输入购买数量: '))
        cart.add_item(product_id, quantity)
    elif choice == '2':
        product_id = int(input('请输入要删除商品的编号: '))
        cart.remove_item(product_id)
    elif choice == '3':
        product_id = int(input('请输入要修改商品数量的编号: '))
        quantity = int(input('请输入新的购买数量: '))
        cart.update_quantity(product_id, quantity)
    elif choice == '4':
        if len(cart.items) == 0:
            print('购物车为空。')
        else:
            print('购物车内容:')
            for item in cart.items:
                product = products[item['product_id'] - 1]
                print(f'商品名称: {product.name}, 数量: {item['quantity']}')
            print(f'总价: {cart.calculate_total_price()} 元')
    elif choice == '5':
        total_price = cart.calculate_total_price()
        discount_price = calculate_discount(total_price)
        print(f'折扣后的价格: {discount_price} 元')
        cart.items = []  # 清空购物车
        print('结算完成!')
    elif choice == '6':
        break
    else:
        print('无效的选择,请重新输入。')

运行结果

用户可以根据提示选择操作,例如添加商品、删除商品、修改商品数量、查看购物车、结算等。程序会根据用户的操作完成相应的步骤,最终输出折扣后的价格和结算信息。

注意事项

  • 以上代码仅供参考,实际应用中需要根据具体的业务需求进行调整。
  • 代码中使用了 input() 函数获取用户输入,需要确保用户输入合法。
  • 代码中使用了列表保存商品信息和购物车信息,需要根据实际情况选择合适的数据结构。
  • 打折规则可以根据实际情况设置,例如满减、折扣等。
  • 代码中没有考虑库存信息,实际应用中需要根据库存信息进行判断。
  • 代码中没有考虑支付环节,实际应用中需要集成支付接口。

希望以上内容能够帮助您了解使用 Python 编写商场周年庆购物打折活动程序的基本步骤和代码实现。

Python 商场周年庆购物打折活动代码示例

原文地址: https://www.cveoy.top/t/topic/ovpV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录