-1

Im trying to make a tip calculator but I'm having trouble learning how to calculate percentage. I want the program to prompt a user to enter how many items they will buy, how much those items cost and what percentage is the tax so it can add them together and give the user an answer.

#Kip Sambu
#2/9/2015
#Assignment 01_04 Word Problems.py


print("")


numItems = raw_input("How many of the item do you want buy? ")



itemCost = raw_input("How much do they cost each?")



discout = raw_input("What is the discount percentage? ")


taxRate = raw_input("What is the sales tax rate? ")

totalCost = (numItems* itemCost)



print "Total Cost = " + str(totalCost)

print
  • Given a total, the k% of that total is (k/100)*total :D. Just be careful about division, have a look here: http://stackoverflow.com/questions/21316968/division-in-python-2-7-and-3-3. Good luck with your studies. – Vincenzo Pii Feb 18 '15 at 16:54
  • So... what is your question? Note that the inputs will be **strings**; if you want numbers, you *have to be explicit about that*. – jonrsharpe Feb 18 '15 at 16:54
  • That's right, `raw_input` will return the input as a string. You can complete it as an integer, by just doing this: `int(raw_input("something: "))`. Since your using percentage, you can change it to a float afterwards too, same way as above. :) – Zizouz212 Feb 18 '15 at 22:37

1 Answers1

1

To calculate certain porcentage of a value do this:

value = baseValue * (Percentage/100)

If the tax rate is 20%, and the value of the purchase is 500 (before taxes) you would calculate the applied taxes as:

taxes = 500 * (20/100)
taxes = 500 * 0.2
taxes = 100

To ge the cost of the purchase (after taxes), you need to add this value to the pre-taxes cost:

finalCost = 500 + 100

You can calculate the final cost directly like this:

finalCost = baseValue * ( 1 + (Percentage/100) )

To get the final value, taking into account a percentage discount, its as simple as:

finalCost = baseValue * (1 - (discountPercentage/100) ) 
Leo
  • 1,034
  • 9
  • 23