Aslan
Aslan

Reputation: 49

calculating sale price based on dependent fees in python

I am trying to figure out what the sale price would be, for an item with a margin of 25%. However there are 2 variables that are dependent on the sale price to compute the price of that particular fee, as shown in the example below:

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)

I get the following Error:


NameError Traceback (most recent call last)

ipython-input-5-2562b4009d70 in module() 1 cost_price = 8.50 * 1.2 2 ----> 3 fee_1 = sale_price * 0.2 4 fee_2 = (sale_price * 0.034) + 0.20 5 fee_3 = 2.90

NameError: name 'sale_price' is not defined

I am still new to programming in python, so any help would be greatly appreciated or a general direction as to where I could possibly head to, in order to arrive at the solution. Will continue working on this until resolved; will report back if I manage to solve the problem.

Upvotes: 1

Views: 615

Answers (3)

Aslan
Aslan

Reputation: 49

Thanks to @Luca for the support in pointing me in the right direction, I have come to a final solution of:

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)

Output:

19.155643564356435, 1.7431635643564363, 10.2, 17.41248

I hope this is of some use to someone other than me =)

Upvotes: 0

Luca Bezerra
Luca Bezerra

Reputation: 1188

Talking strictly business, you can't calculate the fees you apply to a product/service based on the sale_price, as the sale_price already includes those fees. You'd be calculating a fee on top of that same fee, virtually doubling it.

All your fees must be calculated with regards to the cost_price or else you should arbitrarily define fee values to be added on top of it. This is how the code should look (as already answered by other people):

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)

UPDATE:

After new info, here's my take: fee_1 and fee_2 cannot be based on the sale_price, or else you gotta call sale_price something else. But naming aside, if you really want them to derive from the so called sale_price, here's how you could do it:

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)

However, I strongly advise against using this kind of logic, as it is not intuitive and can be very confusing. I believe you should, at the very least, rename your variables and call sale_price something like partial_sale_price on line 7.

Upvotes: 1

alex067
alex067

Reputation: 3301

You have not declared the variable sale_price anywhere. So the compiler doesn't know what to do next.

Did you mean to use cost_price instead?

Upvotes: 0

Related Questions