Using Basic Operators to Compute Sales Tax, Tip, and Total Bill Example Python Programming

Using Basic Operators to Compute Sales Tax, Tip, and Total Bill

To put your knowledge of variables, data types, and operators to good use, you can design a simple program that will compute the sales tax and tip on a restaurant meal.

Meal cost
Sales tax rate
20% of meal + tax

First, set up a variable meal to store the food cost:

meal = 65.50

Next, set up the tax and tip variable. Assign both variables the decimal value of the percentages given. You can do this by using 100 as divisor.

tax = 6.6 / 100

tip = 20 / 100

Your tip is based on meal cost and the added sales tax so you need to get the total amount of the meal and the sales tax. One way to do this is by simply creating a new variable to store the total cost of the meal and tax. Another way is by reassigning the variable meal so that it stores both values:

meal = meal + meal * tax

Now that you have reassigned meal to take care of the meal cost and tax, you’re ready to compute for the tip. This time, you can set a new variable to store the value of the tip, meal, and tax. You can use the variable total to hold all values:

total = meal * tip

Here’s your code to compute for the total bill amount:

meal = 65.50

tax = 6.6 / 100

tip = 20 / 100

meal = meal + meal * tax

total = meal + meal * tip

If you’re using the file editor in IDLE, you can save the file in a filename of your choice and Python automatically appends the .py extension. As you may have noticed, the file editor will always prompt you to save your file before it does anything about your code. Just like when naming other data files and types, you should use a filename that’s descriptive of the file. In this case, a filename like BillCalculator should do the trick.

To get the total amount, go to the Python Shell and type total:



Now you have the bill amount: 83.78760000000001

If you’re using the line command window, you can simply enter the above code on a per line basis.

This simple program shows how straightforward Python programming is and how useful it could be in automating tasks. Next time you eat out, you can reuse the program by simply changing the figures on your bill calculator. Think forward and visualize how convenient it could be if you could put your code in a bigger program that will simply ask you to input the bill amount instead of accessing the original code. You can do that with Python.