Help with some python homework...
Denis McMahon
denismfmcmahon at gmail.com
Sat Feb 1 14:45:32 EST 2014
On Fri, 31 Jan 2014 22:18:34 -0700, Scott W Dunning wrote:
> Any chance you guys could help with another question I have? Below is a
> code to a different problem. The only thing I don’t understand is why
> when calculating the 'discounted price’ you have to subtract 1? Thanks
> again guys!
price_per_book = 24.95
discount = .40
quantity = 60
discounted_price = (1-discount) * price_per_book
shipping = 3.0 + (60 - 1) * .75
total_price = 60 * discounted_price + shipping
print total_price, 'Total price'
You subtract 1 from the shipping price (which should be quantity - 1) to
allow for the fact that the first book costs 3.0 snargles to ship, and
extra books in the same shipment cost 0.75 snargles each.
So if the quantity is greater than one, the shipping cost is 3 snargles
for the first book plus 0.75 snargles times (quantity minus one)
The discounted price needs to be the cost, not the discount.
If the discount is 0.4 (or 40%), then the cost is 0.6 (or 60%) of the
list price. You are trying to calculate the cost, not the discount.
list_price = discounted_price + discount_amount
1.0 * list_price = 0.6 * list_price + 0.4 * list_price
Hence:
discounted_price = list_price - discount_amount
0.6 * list_price = 1.0 * list_price - 0.4 * list_price
so discounted_price = ( 1.0 - 0.4 ) * list_price
where 0.4 is the decimal fraction of the discount
--
Denis McMahon, denismfmcmahon at gmail.com
More information about the Python-list
mailing list