【问题标题】:Calculating a price of a bulk order - formula/calculation help requested计算批量订单的价格 - 请求公式/计算帮助
【发布时间】:2021-05-28 01:34:32
【问题描述】:

我是 python 新手,在我鼓起的一个练习程序上有点挣扎。

目标

如果数量为 100 件或更少,则每件成本为 350 美元。

如果数量为 200 件或更少,前 100 件的价格为 350 美元,不限 不到 200,但超过 100 的成本是 $300。

如果数量为 200 件或更多,前 100 件售价 350 美元,下一件 100 件要 300 美元,200 件以上要 250 美元。

计算总订单的价格。


quantities = [84, 100, 126, 150, 186, 200, 216, 248]

cost = 0

for i in range(len(quantities)):

    if quantities[i] <= 100:

        cost = (quantities[i] * 350)

        print(f"The cost for an order of {quantities[i]} is ${cost}.")

    elif quantities[i] >= 100 and quantities[i] <= 200:

        cost = cost + (quantities[i] * 300)

        print(f"The cost for an order of {quantities[i]} is ${cost}.")

    elif quantities[i] >= 200:

        cost = cost + (quantities[i] * 250)

        print(f"The cost for an order of {quantities[i]} is ${cost}.")

我得到的答案:

The cost for an order of 84 is $29400. 

The cost for an order of 100 is $35000. 

The cost for an order of 126 is $72800. 

The cost for an order of 150 is $117800. 

The cost for an order of 186 is $173600. 

The cost for an order of 200 is $233600. 

The cost for an order of 216 is $287600. 

The cost for an order of 248 is $349600.

我应该得到的答案:

The cost for an order of 84 is $29400. 

The cost for an order of 100 is $35000. 

The cost for an order of 126 is $42800. 

The cost for an order of 150 is $50000. 

The cost for an order of 186 is $60800. 

The cost for an order of 200 is $65000. 

The cost for an order of 216 is $69000. 

The cost for an order of 248 is $77000.

Where am I going wrong in my calculation?

【问题讨论】:

    标签: python


    【解决方案1】:

    这里有两个主要问题。

    1. 您在块中使用cost = cost + ... 语句,但您使用的是elif。这意味着这三个块中只有 一个 将在循环的每次迭代中执行。您也不要在循环的每次迭代中将 cost 重置为 0

    2. 您将成本应用于订单的全部数量,并错过了您在需求中调用的逻辑,仅将其应用于特定范围

    如果我正确理解您的要求,计算成本应该如下所示

    def calc_cost(quantity):
         if quantity <= 100:
             return 350 * quantity
         elif quantity > 100 and quantity <= 200:
             return 100 * 350 + (quantity - 100) * 300
         else:
             return 100 * 350 + 100 * 300 + (quantity - 200) * 250
    

    然后您可以使用以下方式显示您的计算

    In [5]: for quantity in quantities:
       ...:     print(f'The cost of an order of {quantity} is {calc_cost(quantity)}')
       ...:
    The cost of an order of 84 is 29400
    The cost of an order of 100 is 35000
    The cost of an order of 126 is 42800
    The cost of an order of 150 is 50000
    The cost of an order of 186 is 60800
    The cost of an order of 200 is 65000
    The cost of an order of 216 is 69000
    The cost of an order of 248 is 77000
    

    (额外的语法帮助) 在python中你不必使用索引进行迭代,你可以很容易地做到for quantity in quantities如上所示

    【讨论】:

    • @Brandon_Davis_OKC 还有一个语法提示:quantity &gt; 100 and quantity &lt;= 200 可以作为100 &lt; quantity &lt;= 200 链接在一起。
    猜你喜欢
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多