In the code below I don't understand what the (1- discount/100.0) is doing, specifically the 1- part? Sorry I am a 2 week old programmer. price = raw_input("Enter the original price: ") discount = raw_input("Enter discount: ") price = int(price) discount = int(discount) def newpr(): newprice = (1- discount/100.0)*price print "The discounted price is: %.2f" %(newprice)e)
The formula is wrong, it should be 100 instead of 1. Imagine there's a TV that costs $500, and you got a 25% discount on it. Saying you have a 25% discount is the same as saying you have to pay the 75% of the price. So, to know the percentage you'll have to pay you substract the discount from 100% So the formula should say 100 - discount. And to know how much is the percentage of a price, you multiply the percentage by the price. Percentage is how you express a number that has as a denominator 100. So 75% is the same as 75/100 which at the same time is the same as .75 In order to use a percentage in a multiplication you have to use either fractions or decimals, but decimals are way easier to use. What this (newprice = (100- discount/100.0)*price) is doing, or should, is substract the discount from 100 to obtain the percentage you have to pay, then it's dividing it by 100 to represent the percentage in decimals, and finally is multiplying it by the original price to obtain what you have to pay after the discount. Following the example of the TV: 100 - 25 = 75 75 / 100 = .75 .75 * 500 = 375
The formula is written like that because you are taking a part of the whole instead of a percentage. If you have 1 of some item, then you have 100% of that item, or as a fraction, you have 100/100. So your discount, expressed as a fraction, is the whole (1) less the fractional portion (discount/100). In the TV example above, take the 1, less 25/100, and multiply by the price (1-25/100)*500 = 375
The discount is the portion of the original price that is not included in the new price. Since the discount is treated as an integer --> discount = int(discount), the discount/100 translate the discount into a decimal. The 1 - part determines the percentage that is included in the new price.
"Sorry I am a 2 week old programmer." \(\leftarrow\) No need to apologize for that! We all had to start somewhere and I am sure everyone has a question or three at times.
Thanks alot everyone!
Join our real-time social learning platform and learn together with your friends!