以下是一个计算商品利润的Python代码示例:

def calculate_profit():
    # 商品种类与利润率的映射关系
    profit_rate = {
        'A': 0.1,
        'B': 0.15,
        'C': 0.2,
        # 添加更多商品种类及利润率
    }

    total_profit = 0

    num_of_items = int(input("请输入商品种类数量:"))

    for i in range(num_of_items):
        item_name = input("请输入商品名称:")
        item_quantity = int(input("请输入商品数量:"))
        item_price = float(input("请输入商品单价:"))

        if item_name in profit_rate:
            profit = item_price * item_quantity * profit_rate[item_name]
            total_profit += profit
            print(f"商品 {item_name} 的利润为:{profit}")
        else:
            print(f"商品 {item_name} 不存在利润率信息,请重新输入。")

    print(f"总利润为:{total_profit}")


calculate_profit()

这段代码首先利用字典profit_rate存储了不同商品种类的利润率。然后通过用户输入商品种类数量,以及每个商品的名称、数量和单价,计算每个商品的利润并累加到总利润中,最后输出总利润。如果输入的商品名称不在profit_rate字典中,则会提示重新输入。请根据实际需求进行适当修改


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

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