我想弄清楚某个利润率为25%的商品的售价是多少。但是,有2个变量取决于销售价格来计算特定费用的价格,如下面的示例所示:
cost_price = 8.50 * 1.2
fee_1 = sale_price * 0.2
fee_2 = (sale_price * 0.066) + 0.60
fee_3 = 2.90
fee_4 = 0.20
cost = fee_1 + fee_2 + cost_price + fee_3 + fee_4
sale_price = cost / (1-0.25)
print(sale_price)
我收到以下错误消息:
NameError跟踪(最近一次通话最近)
module()中的ipython-input-5-2562b4009d70 1 cost_price = 8.50 * 1.2 2 ----> 3 Fee_1 = sale_price * 0.2 4费用_2 =(促销价格* 0.034)+ 0.20 5 fee_3 = 2.90
NameError:名称'sale_price'未定义
我仍然不熟悉python编程,因此将不胜感激,或者对我可能到达的位置的一般指导,以寻求解决方案。在解决之前将继续努力;如果我能够解决问题,系统会向您报告。
答案 0 :(得分:1)
严格来讲,您无法基于sale_price
计算出您对某项产品/服务收取的费用,因为sale_price
已包含这些费用。您将在相同费用的基础上计算费用,实际上是将费用加倍。
您必须针对cost_price
计算所有费用,否则您应随意定义要添加在其顶部的费用值。这是代码的外观(其他人已经回答了):
cost_price = 8.50 * 1.2
fee_1 = cost_price * 0.2
fee_2 = (cost_price * 0.066) + 0.60
fee_3 = 2.90
fee_4 = 0.20
cost = fee_1 + fee_2 + cost_price + fee_3 + fee_4
sale_price = cost / (1-0.25)
print(sale_price)
更新:
获取新信息后,我的看法是:fee_1
和fee_2
不能基于sale_price
,否则您将不得不称呼sale_price
。但顺便说一句,如果您真的希望它们从所谓的sale_price
派生,那么您可以这样做:
cost_price = 8.50 * 1.2
fee_3 = 2.90
fee_4 = 0.20
cost = cost_price + fee_3 + fee_4
sale_price = cost / (1-0.25) # line 7 -- btw, why not use 0.75 instead of 1 - 0.25?
# now that sale_price exists, we can generate the fees based on it
fee_1 = sale_price * 0.2
fee_2 = (sale_price * 0.066) + 0.60
sale_price += fee_1 + fee_2
print(sale_price)
但是,我强烈建议您不要使用这种逻辑,因为它不直观,可能会造成混乱。我认为您至少应该重命名变量,并在第7行调用sale_price
之类的partial_sale_price
。
答案 1 :(得分:0)
您尚未在任何地方声明变量sale_price
。因此,编译器不知道下一步该怎么做。
您是要改用cost_price
吗?
答案 2 :(得分:0)
感谢@Luca为我指出正确方向的支持,我得出了以下最终解决方案:
cost_price = 8.50 * 1.2
fee_3 = 2.90
fee_4 = 0.18
cost = cost_price + fee_3 + fee_4
sale_price = cost
fee_1 = sale_price * 0.2
fee_2 = (sale_price * 0.066) + 0.60
final_cost = fee_1 + fee_2 + cost
final_sale_price = final_cost / (1-0.081)
profit = final_sale_price - final_cost
print(final_sale_price, profit, purchase_price, final_cost)
输出:
19.155643564356435、1.7431635643564363、10.2、17.41248
我希望这对我以外的人有帮助=)